mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-19 15:02:15 +01:00
Merge main into feat/tapes-from-blackbox
Conflicts: - CabPanel.cs: main (#50) tells the screen reader what the panel draws, this branch keeps the km/h label for the speed of the wheels beside it and shows the doors as the cab sees them. Both kept, and the speed of the wheels, new on this branch, is told to the screen reader too, as all the rest. - Records.cs: main (#55) adds the buzzers and the protection braking, this branch AsTheCabSees. Both kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
125
StarterNG.Tests/AnnouncerTests.cs
Normal file
125
StarterNG.Tests/AnnouncerTests.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using Avalonia.Automation;
|
||||
using Avalonia.Automation.Peers;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.VisualTree;
|
||||
using StarterNG.Controls;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class AnnouncerTests
|
||||
{
|
||||
private DateTime _now = new(2026, 11, 15, 12, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
private Announcer NewAnnouncer() => new(() => _now);
|
||||
|
||||
[Fact]
|
||||
public void A_message_is_read_while_fresh_and_then_forgotten()
|
||||
{
|
||||
var announcer = NewAnnouncer();
|
||||
announcer.Say(" Jakość grafiki: Wysoka ");
|
||||
Assert.Equal("Jakość grafiki: Wysoka", announcer.Message);
|
||||
|
||||
_now += Announcer.Shown;
|
||||
Assert.Null(announcer.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Nothing_to_say_says_nothing()
|
||||
{
|
||||
var announcer = NewAnnouncer();
|
||||
announcer.Say("Zapisano ustawienia");
|
||||
announcer.Say(" ");
|
||||
|
||||
Assert.Equal("Zapisano ustawienia", announcer.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_control_inside_says_it_through_the_window_announcer()
|
||||
{
|
||||
var button = new Button();
|
||||
var announcer = NewAnnouncer();
|
||||
announcer.Child = new Border { Child = button };
|
||||
|
||||
Announcer.Say(button, "Godzina: 07:05");
|
||||
|
||||
Assert.Equal("Godzina: 07:05", announcer.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_control_outside_any_announcer_is_not_heard()
|
||||
{
|
||||
var button = new Button();
|
||||
|
||||
var thrown = Record.Exception(() => Announcer.Say(button, "Godzina: 07:05"));
|
||||
|
||||
Assert.Null(thrown);
|
||||
Assert.Null(button.FindAncestorOfType<Announcer>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_marked_text_is_read_when_it_changes()
|
||||
{
|
||||
var status = new TextBlock();
|
||||
Announcer.SetChanges(status, true);
|
||||
var plain = new TextBlock();
|
||||
var announcer = NewAnnouncer();
|
||||
announcer.Child = new StackPanel { Children = { status, plain } };
|
||||
|
||||
plain.Text = "Ścieżka";
|
||||
Assert.Null(announcer.Message);
|
||||
|
||||
status.Text = "Niezapisane zmiany";
|
||||
Assert.True(Announcer.GetChanges(status));
|
||||
Assert.Equal("Niezapisane zmiany", announcer.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_screen_reader_hears_it_as_a_polite_live_region()
|
||||
{
|
||||
var announcer = NewAnnouncer();
|
||||
var peer = ControlAutomationPeer.CreatePeerForElement(announcer);
|
||||
announcer.Say("Przywrócono domyślne");
|
||||
|
||||
Assert.Equal("Przywrócono domyślne", peer.GetName());
|
||||
Assert.Equal(AutomationLiveSetting.Polite, peer.GetLiveSetting());
|
||||
Assert.Equal(AutomationControlType.Group, peer.GetAutomationControlType());
|
||||
}
|
||||
}
|
||||
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class NamedGroupTests
|
||||
{
|
||||
static NamedGroupTests() => ScreenReaderNames.Install();
|
||||
|
||||
[Fact]
|
||||
public void A_panel_with_a_title_is_a_group_named_by_it()
|
||||
{
|
||||
var panel = new HeaderedContentControl { Header = "Scenerie" };
|
||||
|
||||
Assert.Equal("Scenerie", AutomationProperties.GetName(panel));
|
||||
Assert.Equal(AutomationControlType.Group, AutomationProperties.GetControlTypeOverride(panel));
|
||||
Assert.Equal(AccessibilityView.Content, AutomationProperties.GetAccessibilityView(panel));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_panel_whose_title_goes_is_no_longer_a_group()
|
||||
{
|
||||
var panel = new HeaderedContentControl { Header = "Skład" };
|
||||
panel.Header = null;
|
||||
|
||||
Assert.Null(AutomationProperties.GetName(panel));
|
||||
Assert.Null(AutomationProperties.GetControlTypeOverride(panel));
|
||||
Assert.Equal(AccessibilityView.Default, AutomationProperties.GetAccessibilityView(panel));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_tab_keeps_its_own_way_of_speaking()
|
||||
{
|
||||
var tab = new TabItem { Header = "Pogoda" };
|
||||
|
||||
Assert.Null(AutomationProperties.GetName(tab));
|
||||
Assert.Equal(AutomationControlType.TabItem, AutomationProperties.GetControlTypeOverride(tab));
|
||||
}
|
||||
}
|
||||
11
StarterNG.Tests/AvaloniaControls.cs
Normal file
11
StarterNG.Tests/AvaloniaControls.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Tests that build Avalonia controls run one after another: a control belongs to the
|
||||
/// thread that made the first one, and run side by side they now and then failed.
|
||||
/// </summary>
|
||||
[CollectionDefinition(Name)]
|
||||
public sealed class AvaloniaControls
|
||||
{
|
||||
public const string Name = "Avalonia controls";
|
||||
}
|
||||
284
StarterNG.Tests/BlackBox/BlackBoxViewsTests.cs
Normal file
284
StarterNG.Tests/BlackBox/BlackBoxViewsTests.cs
Normal file
@@ -0,0 +1,284 @@
|
||||
using Avalonia.Automation;
|
||||
using Avalonia.Automation.Peers;
|
||||
using Avalonia.Automation.Provider;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.VisualTree;
|
||||
using Eu07.BlackBox;
|
||||
using Eu07.BlackBox.Views;
|
||||
|
||||
namespace StarterNG.Tests.BlackBox;
|
||||
|
||||
/// <summary>
|
||||
/// The black box's own drawn panels as a screen reader user meets them: reached with Tab,
|
||||
/// walked with the keys, their value and description read, what they show said aloud.
|
||||
/// </summary>
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class BlackBoxViewsTests
|
||||
{
|
||||
private const uint Head = 1;
|
||||
|
||||
/// <summary>
|
||||
/// The user in vehicle 1 from t = 0 to 120: timetable ROJ1 standing at A, stopping at B
|
||||
/// from 60 to 70, passing C at 100; the clock at 10:34 (634 min).
|
||||
/// </summary>
|
||||
private static Recording Journey(bool extended = true, byte? engine = null)
|
||||
{
|
||||
var f = new BlackBoxFile().Info()
|
||||
.Vehicle(Head, "EU07-005", "ROJ1")
|
||||
.User(0, Head, true, 1)
|
||||
.Timetable(0, Head, "ROJ1", "A", "C", ("A", -1, 634), ("Stacja_B", 635, 635.5f), ("C", 637.5f, 638))
|
||||
.Station(1, Head, 4, 1, "A", 1150);
|
||||
for (double t = 0; t <= 120; t += 0.5)
|
||||
{
|
||||
float speed = t is >= 55 and < 70 ? 0 : 40;
|
||||
f.Cab(t, Head, 1150 + t * 10, speed, extended: extended, engine: engine, nextLimit: 60, nextDistance: 500,
|
||||
brakeCylinder: t is >= 50 and < 70 ? 2.5f : 0, devices: 0x0207);
|
||||
}
|
||||
f.Station(60, Head, 1, 2, "Stacja_B", 1300)
|
||||
.Station(100, Head, 3, 3, "C", 2150);
|
||||
return BlackBoxReader.Read(f.End(120).Stream());
|
||||
}
|
||||
|
||||
private static IValueProvider Value(Control control) =>
|
||||
Assert.IsAssignableFrom<IValueProvider>(Controls.Peer(control));
|
||||
|
||||
// --- timeline ---
|
||||
|
||||
[Fact]
|
||||
public void The_timeline_is_a_slider_read_as_the_moment_with_how_to_walk_it()
|
||||
{
|
||||
var timeline = new TimelineView { Spoken = "10:35:12, 40 km/h" };
|
||||
var peer = Controls.Peer(timeline);
|
||||
var value = Value(timeline);
|
||||
|
||||
Assert.Equal(AutomationControlType.Slider, peer.GetAutomationControlType());
|
||||
Assert.Equal("10:35:12, 40 km/h", value.Value);
|
||||
Assert.True(value.IsReadOnly);
|
||||
Assert.False(string.IsNullOrEmpty(peer.GetHelpText()));
|
||||
Assert.True(peer.IsContentElement());
|
||||
Assert.True(peer.IsControlElement());
|
||||
Assert.Throws<InvalidOperationException>(() => value.SetValue("x"));
|
||||
|
||||
timeline.Spoken = "10:35:22, 40 km/h";
|
||||
Assert.Equal("10:35:22, 40 km/h", value.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_description_set_on_the_timeline_wins_over_its_own()
|
||||
{
|
||||
var timeline = new TimelineView();
|
||||
AutomationProperties.SetHelpText(timeline, "own words");
|
||||
|
||||
Assert.Equal("own words", Controls.Peer(timeline).GetHelpText());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(Key.Right, KeyModifiers.None, 70)]
|
||||
[InlineData(Key.Left, KeyModifiers.None, 50)]
|
||||
[InlineData(Key.Right, KeyModifiers.Control, 120)]
|
||||
[InlineData(Key.Left, KeyModifiers.Control, 0)]
|
||||
[InlineData(Key.PageDown, KeyModifiers.None, 120)]
|
||||
[InlineData(Key.PageUp, KeyModifiers.None, 0)]
|
||||
[InlineData(Key.Home, KeyModifiers.None, 0)]
|
||||
[InlineData(Key.End, KeyModifiers.None, 120)]
|
||||
public void The_keys_take_the_timeline_through_the_recording(Key key, KeyModifiers modifiers, double expected)
|
||||
{
|
||||
var timeline = new TimelineView { Recording = Journey(), Time = 60 };
|
||||
double? sought = null;
|
||||
timeline.Seek += t => sought = t;
|
||||
|
||||
Assert.True(Controls.Press(timeline, key, modifiers));
|
||||
Assert.Equal(expected, sought);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Other_keys_and_an_empty_timeline_are_left_alone()
|
||||
{
|
||||
var empty = new TimelineView();
|
||||
var timeline = new TimelineView { Recording = Journey(), Time = 60 };
|
||||
bool sought = false;
|
||||
empty.Seek += _ => sought = true;
|
||||
timeline.Seek += _ => sought = true;
|
||||
|
||||
Assert.False(Controls.Press(empty, Key.Right));
|
||||
Assert.False(Controls.Press(timeline, Key.A));
|
||||
Assert.False(sought);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_timeline_draws_with_and_without_the_focus()
|
||||
{
|
||||
var timeline = new TimelineView { Recording = Journey(), Time = 60 };
|
||||
var window = Controls.Show(timeline, 900, 300);
|
||||
try
|
||||
{
|
||||
timeline.Fit();
|
||||
Controls.Draw(timeline);
|
||||
Assert.True(timeline.IsFocused);
|
||||
timeline.Time = 119;
|
||||
Controls.Draw(timeline);
|
||||
window.Focus();
|
||||
Controls.Draw(timeline);
|
||||
}
|
||||
finally
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
// --- timetable ---
|
||||
|
||||
[Fact]
|
||||
public void The_timetable_is_a_list_read_row_by_row_from_the_keys()
|
||||
{
|
||||
var table = new TimetablePanel { Recording = Journey(), Time = 30 };
|
||||
var peer = Controls.Peer(table);
|
||||
var value = Value(table);
|
||||
|
||||
Assert.Equal(AutomationControlType.List, peer.GetAutomationControlType());
|
||||
Assert.False(string.IsNullOrEmpty(peer.GetHelpText()));
|
||||
Assert.StartsWith("1/3. A", value.Value);
|
||||
|
||||
Assert.True(Controls.Press(table, Key.Down));
|
||||
Assert.StartsWith("2/3. Stacja B", value.Value);
|
||||
Assert.True(Controls.Press(table, Key.Up));
|
||||
Assert.StartsWith("1/3. A", value.Value);
|
||||
Assert.True(Controls.Press(table, Key.End));
|
||||
Assert.StartsWith("3/3. C", value.Value);
|
||||
Assert.True(Controls.Press(table, Key.Down));
|
||||
Assert.StartsWith("3/3. C", value.Value);
|
||||
Assert.True(Controls.Press(table, Key.Home));
|
||||
Assert.StartsWith("1/3. A", value.Value);
|
||||
Assert.Contains("km 2.5", value.Value.Replace(',', '.'));
|
||||
Assert.False(Controls.Press(table, Key.A));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Enter_on_a_station_passed_goes_to_a_little_before_it()
|
||||
{
|
||||
var table = new TimetablePanel { Recording = Journey(), Time = 110 };
|
||||
double? sought = null;
|
||||
table.Seek += t => sought = t;
|
||||
|
||||
Controls.Press(table, Key.Home);
|
||||
Controls.Press(table, Key.Down);
|
||||
|
||||
Assert.True(Controls.Press(table, Key.Enter));
|
||||
Assert.Equal(55, sought);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_recording_without_a_timetable_says_so_and_ignores_the_keys()
|
||||
{
|
||||
var recording = BlackBoxReader.Read(new BlackBoxFile().Info().Vehicle(Head, "EU07-005")
|
||||
.Cab(0, Head, 1150, 40).End(1).Stream());
|
||||
var table = new TimetablePanel { Recording = recording, Time = 0 };
|
||||
|
||||
Assert.False(string.IsNullOrEmpty(Value(table).Value));
|
||||
Assert.False(Controls.Press(table, Key.Down));
|
||||
table.Recording = null;
|
||||
Controls.Lay(table);
|
||||
Controls.Draw(table);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_timetable_draws_its_rows_and_the_one_the_keys_stand_at()
|
||||
{
|
||||
var table = new TimetablePanel { Recording = Journey(), Time = 65 };
|
||||
var window = Controls.Show(table, 600, 400);
|
||||
try
|
||||
{
|
||||
Controls.Draw(table);
|
||||
Controls.Press(table, Key.Down);
|
||||
Controls.Draw(table);
|
||||
window.Focus();
|
||||
Controls.Draw(table);
|
||||
}
|
||||
finally
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
// --- cab ---
|
||||
|
||||
[Theory]
|
||||
[InlineData(true, null)]
|
||||
[InlineData(true, (byte)1)]
|
||||
[InlineData(true, (byte)2)]
|
||||
[InlineData(false, null)]
|
||||
public void The_cab_says_what_it_draws(bool extended, byte? engine)
|
||||
{
|
||||
var cab = new CabPanel { Recording = Journey(extended, engine), Time = 30 };
|
||||
Controls.Lay(cab, 600, 900);
|
||||
|
||||
Controls.Draw(cab);
|
||||
|
||||
Assert.NotNull(cab.Said);
|
||||
Assert.Contains("EU07-005", cab.Said);
|
||||
Assert.Contains("40 km/h", cab.Said);
|
||||
Assert.Equal(AutomationControlType.Pane, Controls.Peer(cab).GetAutomationControlType());
|
||||
Assert.Equal(cab.Said, Controls.Peer(cab).GetHelpText());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_cab_stopped_under_the_brakes_and_out_of_the_recording()
|
||||
{
|
||||
var cab = new CabPanel { Recording = Journey(), Time = 60 };
|
||||
var window = Controls.Show(cab, 600, 900);
|
||||
try
|
||||
{
|
||||
Controls.Draw(cab);
|
||||
Assert.Contains("0 km/h", cab.Said);
|
||||
|
||||
cab.Time = 500;
|
||||
Controls.Draw(cab);
|
||||
Assert.DoesNotContain("km/h", cab.Said);
|
||||
}
|
||||
finally
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_empty_cab_says_there_is_nothing_to_show()
|
||||
{
|
||||
var cab = new CabPanel();
|
||||
Controls.Lay(cab);
|
||||
|
||||
Controls.Draw(cab);
|
||||
|
||||
Assert.False(string.IsNullOrEmpty(cab.Said));
|
||||
}
|
||||
|
||||
// --- segmented choice ---
|
||||
|
||||
[Fact]
|
||||
public void A_segmented_choice_is_a_row_of_tabs_one_of_them_selected()
|
||||
{
|
||||
var choice = new UiKit.Segmented("Mapa", "Profil", "Rozkład");
|
||||
int? picked = null;
|
||||
choice.SelectedChanged += i => picked = i;
|
||||
var tabs = choice.GetVisualDescendants().OfType<Button>().ToList();
|
||||
if (tabs.Count == 0)
|
||||
tabs = ((Panel)choice.Child!).Children.OfType<Button>().ToList();
|
||||
|
||||
var second = Assert.IsAssignableFrom<ISelectionItemProvider>(Controls.Peer(tabs[1]));
|
||||
Assert.Equal(AutomationControlType.TabItem, Controls.Peer(tabs[1]).GetAutomationControlType());
|
||||
Assert.Null(second.SelectionContainer);
|
||||
|
||||
second.Select();
|
||||
Assert.Equal(1, picked);
|
||||
Assert.True(second.IsSelected);
|
||||
Assert.Equal(1, choice.Selected);
|
||||
|
||||
var third = Assert.IsAssignableFrom<ISelectionItemProvider>(Controls.Peer(tabs[2]));
|
||||
third.AddToSelection();
|
||||
Assert.Equal(2, choice.Selected);
|
||||
Assert.False(second.IsSelected);
|
||||
third.RemoveFromSelection();
|
||||
Assert.Equal(2, choice.Selected);
|
||||
}
|
||||
}
|
||||
43
StarterNG.Tests/BlackBox/EventTextsTests.cs
Normal file
43
StarterNG.Tests/BlackBox/EventTextsTests.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Eu07.BlackBox;
|
||||
using Eu07.BlackBox.Views;
|
||||
|
||||
namespace StarterNG.Tests.BlackBox;
|
||||
|
||||
public class EventTextsTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("pl", "aidriverenable", "Aktywacja automechanika")]
|
||||
[InlineData("en", "aidriverenable", "Engage the AI driver")]
|
||||
[InlineData("pl", "mastercontrollerset", "Ustawienie nastawnika głównego")]
|
||||
[InlineData("pl", "headlightenablerearleft", "Włączenie lewego reflektora z tyłu")]
|
||||
[InlineData("cz", "batteryenable", "Turn the battery on")]
|
||||
public void A_command_is_told_by_what_it_does(string language, string command, string text) =>
|
||||
Assert.Equal(text, new DefaultBlackBoxStrings(language)[DefaultBlackBoxStrings.CommandPrefix + command]);
|
||||
|
||||
[Fact]
|
||||
public void A_command_with_no_text_keeps_its_name() =>
|
||||
Assert.Equal("somethingnew", new DefaultBlackBoxStrings("pl")[DefaultBlackBoxStrings.CommandPrefix + "somethingnew"]);
|
||||
|
||||
[Fact]
|
||||
public void Every_command_has_both_texts_starting_with_a_capital()
|
||||
{
|
||||
foreach (string command in CommandTexts.Commands)
|
||||
foreach (string language in new[] { "pl", "en" })
|
||||
{
|
||||
string text = CommandTexts.Of(command, language);
|
||||
Assert.NotEqual(command, text);
|
||||
Assert.True(char.IsUpper(text[0]), $"{command} ({language}): {text}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_events_name_the_commands_by_what_they_do()
|
||||
{
|
||||
var recording = BlackBoxReader.Read(new BlackBoxFile().Info()
|
||||
.Vehicle(1, "EP07-424").User(0, 1, true).Command(12.0, 1, true, "batteryenable").Stream());
|
||||
|
||||
var events = EventList.Collect(recording, new DefaultBlackBoxStrings("pl"));
|
||||
|
||||
Assert.Contains(events, e => e.Text == "Włączenie baterii");
|
||||
}
|
||||
}
|
||||
88
StarterNG.Tests/BlackBox/SecurityBrakingTests.cs
Normal file
88
StarterNG.Tests/BlackBox/SecurityBrakingTests.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System.Globalization;
|
||||
using Eu07.BlackBox;
|
||||
using Eu07.BlackBox.Views;
|
||||
|
||||
namespace StarterNG.Tests.BlackBox;
|
||||
|
||||
public class SecurityBrakingTests
|
||||
{
|
||||
// as the simulator writes them: one acknowledgement for both, so the cab signalling's buzzer is the alerter's too
|
||||
private const byte CabSignal = 0x02, CabSignalBuzzing = 0x0e, CabSignalBraking = 0x1e, NoPower = 0x10;
|
||||
|
||||
private static Recording Drive(System.Func<double, byte> security)
|
||||
{
|
||||
var file = new BlackBoxFile().Info().Vehicle(1, "EP07-424").User(0, 1, true);
|
||||
for (double t = 0; t <= 20; t += 0.5)
|
||||
file.Cab(t, 1, 100, 0, security: security(t));
|
||||
return BlackBoxReader.Read(file.Stream());
|
||||
}
|
||||
|
||||
private static List<string> Events(Recording recording)
|
||||
{
|
||||
var culture = CultureInfo.CurrentCulture;
|
||||
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
try
|
||||
{
|
||||
return EventList.Collect(recording, new DefaultBlackBoxStrings("pl")).Select(e => e.Text).ToList();
|
||||
}
|
||||
finally
|
||||
{
|
||||
CultureInfo.CurrentCulture = culture;
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_protection_braking_with_no_power_is_not_the_drivers_fault()
|
||||
{
|
||||
// no power for the first seconds, then the cab signalling not acknowledged
|
||||
var recording = Drive(t => t < 5 ? NoPower : t is >= 10 and < 12 ? (byte)0x1a : (byte)0);
|
||||
|
||||
var braking = Assert.Single(recording.SecurityBrakings());
|
||||
Assert.Equal(10, braking.Start, 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_buzzer_acknowledged_in_time_is_told_apart_from_braking()
|
||||
{
|
||||
var recording = Drive(t => t is >= 2 and < 4 ? CabSignal : t is >= 4 and < 5.5 ? CabSignalBuzzing : (byte)0);
|
||||
|
||||
var events = Events(recording);
|
||||
|
||||
Assert.Contains("Brzęczyk SHP, potwierdzony po 1.5 s", events);
|
||||
Assert.DoesNotContain(events, e => e.StartsWith("HAMOWANIE", System.StringComparison.Ordinal));
|
||||
Assert.Empty(recording.SecurityBrakings());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_buzzer_cut_by_the_power_is_not_acknowledged()
|
||||
{
|
||||
var recording = Drive(t => t is >= 2 and < 4 ? CabSignalBuzzing : t is >= 4 and < 8 ? NoPower : (byte)0);
|
||||
|
||||
var events = Events(recording);
|
||||
|
||||
Assert.Contains("Brzęczyk SHP", events);
|
||||
Assert.DoesNotContain(events, e => e.Contains("potwierdzony", System.StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_buzzer_the_other_joins_is_named_by_both()
|
||||
{
|
||||
// the alerter's buzzer, then a magnet passed before it's acknowledged
|
||||
var recording = Drive(t => t is >= 2 and < 4 ? (byte)0x05 : t is >= 4 and < 5 ? (byte)0x0f : (byte)0);
|
||||
|
||||
Assert.Contains("Brzęczyk CA/SHP, potwierdzony po 3.0 s", Events(recording));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_buzzer_not_acknowledged_is_followed_by_the_braking()
|
||||
{
|
||||
// the cab signalling at the start: the buzzer at once, braking when not acknowledged
|
||||
var recording = Drive(t => t < 3 ? CabSignalBuzzing : t < 6 ? CabSignalBraking : (byte)0);
|
||||
|
||||
var events = Events(recording);
|
||||
|
||||
Assert.Equal(["Brzęczyk SHP, bez potwierdzenia", "HAMOWANIE PRZEZ SHP"],
|
||||
events.Where(e => e.Contains("SHP", System.StringComparison.Ordinal)));
|
||||
Assert.DoesNotContain(events, e => e.Contains("CA", System.StringComparison.Ordinal));
|
||||
}
|
||||
}
|
||||
119
StarterNG.Tests/CaseInsensitivePathsTests.cs
Normal file
119
StarterNG.Tests/CaseInsensitivePathsTests.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using StarterNG.Infrastructure;
|
||||
using StarterNG.Tests.Fakes;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
public class CaseInsensitivePathsTests
|
||||
{
|
||||
private static InMemoryFileSystem Disk() => new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile("game/scenery/Braniewo/Base.ctr", "")
|
||||
.WithFile("game/scenery/Braniewo/rozklady/TGS3657.txt", "")
|
||||
.WithDirectory("game/textures/mini");
|
||||
|
||||
[Theory]
|
||||
[InlineData("braniewo/base.ctr", "Braniewo/Base.ctr")]
|
||||
[InlineData(@"BRANIEWO\Rozklady\tgs3657.TXT", "Braniewo/rozklady/TGS3657.txt")]
|
||||
[InlineData("Braniewo/Base.ctr", "Braniewo/Base.ctr")]
|
||||
[InlineData("braniewo//base.ctr", "Braniewo/Base.ctr")]
|
||||
[InlineData("/braniewo/base.ctr", "Braniewo/Base.ctr")]
|
||||
[InlineData(@"\Braniewo\Base.ctr", "Braniewo/Base.ctr")]
|
||||
public void A_name_in_any_case_is_given_as_it_is_on_disk(string name, string onDisk) =>
|
||||
Assert.Equal(onDisk, new CaseInsensitivePaths(Disk()).Resolve("game/scenery", name));
|
||||
|
||||
[Theory]
|
||||
[InlineData("braniewo/brak.ctr")]
|
||||
[InlineData("brak/base.ctr")]
|
||||
[InlineData("")]
|
||||
public void A_name_nothing_answers_to_is_not_found(string name) =>
|
||||
Assert.Null(new CaseInsensitivePaths(Disk()).Resolve("game/scenery", name));
|
||||
|
||||
[Fact]
|
||||
public void A_folder_is_found_only_when_one_is_asked_for()
|
||||
{
|
||||
var names = new CaseInsensitivePaths(Disk());
|
||||
|
||||
Assert.Equal("Braniewo/rozklady", names.Resolve("game/scenery", "braniewo/ROZKLADY", directory: true));
|
||||
Assert.Null(names.Resolve("game/scenery", "braniewo/ROZKLADY"));
|
||||
Assert.Null(names.Resolve("game/scenery", "braniewo/base.ctr", directory: true));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_name_written_as_on_disk_wins_over_one_in_other_case()
|
||||
{
|
||||
var files = Disk().WithFile("game/scenery/braniewo/base.ctr", "");
|
||||
var names = new CaseInsensitivePaths(files);
|
||||
|
||||
Assert.Equal("braniewo/base.ctr", names.Resolve("game/scenery", "braniewo/base.ctr"));
|
||||
Assert.Equal("Braniewo/Base.ctr", names.Resolve("game/scenery", "Braniewo/base.CTR"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Of_folders_named_alike_the_one_holding_the_file_is_taken()
|
||||
{
|
||||
var files = Disk().WithFile("game/scenery/braniewo/tory.scm", "");
|
||||
var names = new CaseInsensitivePaths(files);
|
||||
|
||||
Assert.Equal("Braniewo/Base.ctr", names.Resolve("game/scenery", "braniewo/base.ctr"));
|
||||
Assert.Equal("braniewo/tory.scm", names.Resolve("game/scenery", "Braniewo/Tory.scm"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_full_path_joins_the_folder_and_the_name_on_disk() =>
|
||||
Assert.Equal(Path.Combine("game/scenery", "Braniewo", "Base.ctr"),
|
||||
new CaseInsensitivePaths(Disk()).Find("game/scenery", "BRANIEWO/BASE.CTR"));
|
||||
|
||||
[Fact]
|
||||
public void A_full_path_is_found_from_its_own_root()
|
||||
{
|
||||
string scenery = Path.GetFullPath(Path.Combine("game", "scenery"));
|
||||
var files = new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile(Path.Combine(scenery, "Braniewo", "Base.ctr"), "");
|
||||
|
||||
Assert.Equal(Path.Combine(scenery, "Braniewo", "Base.ctr"),
|
||||
new CaseInsensitivePaths(files).Find("elsewhere", Path.Combine(scenery, "BRANIEWO", "base.CTR")));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_disk_that_ignores_case_gives_the_name_back_as_written()
|
||||
{
|
||||
var files = new InMemoryFileSystem().WithFile("game/scenery/Braniewo/Base.ctr", "");
|
||||
var names = new CaseInsensitivePaths(files);
|
||||
|
||||
Assert.Equal("BRANIEWO/base.CTR", names.Resolve("game/scenery", "BRANIEWO/base.CTR"));
|
||||
Assert.Null(names.Resolve("game/scenery", "braniewo/brak.ctr"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Kept_listings_hold_until_forgotten()
|
||||
{
|
||||
var files = Disk();
|
||||
var names = new CaseInsensitivePaths(files, keepListings: true);
|
||||
Assert.Null(names.Resolve("game/scenery", "braniewo/nowy.scm"));
|
||||
|
||||
files.WithFile("game/scenery/Braniewo/Nowy.scm", "");
|
||||
Assert.Null(names.Resolve("game/scenery", "braniewo/nowy.scm"));
|
||||
|
||||
names.Forget();
|
||||
Assert.Equal("Braniewo/Nowy.scm", names.Resolve("game/scenery", "braniewo/nowy.scm"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Listings_not_kept_see_a_file_added_since()
|
||||
{
|
||||
var files = Disk();
|
||||
var names = new CaseInsensitivePaths(files);
|
||||
Assert.Null(names.Resolve("game/scenery", "braniewo/nowy.scm"));
|
||||
|
||||
files.WithFile("game/scenery/Braniewo/Nowy.scm", "");
|
||||
|
||||
Assert.Equal("Braniewo/Nowy.scm", names.Resolve("game/scenery", "braniewo/nowy.scm"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_folder_that_cannot_be_listed_holds_nothing_to_find()
|
||||
{
|
||||
var files = Disk().DenyListing("game/scenery");
|
||||
|
||||
Assert.Null(new CaseInsensitivePaths(files).Resolve("game/scenery", "braniewo/base.ctr"));
|
||||
}
|
||||
}
|
||||
101
StarterNG.Tests/ConsistNarrationTests.cs
Normal file
101
StarterNG.Tests/ConsistNarrationTests.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using StarterNG.Views;
|
||||
using Unit = StarterNG.Views.ConsistNarration.Unit;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
public class ConsistNarrationTests
|
||||
{
|
||||
// Each text shows its key and its arguments, so a test sees which sentence was picked and with what.
|
||||
private static string Loc(string key) => key switch
|
||||
{
|
||||
"ConsistSaidEmpty" => "empty",
|
||||
"ConsistSaidAdded" => "added {0} at {1}/{2}",
|
||||
"ConsistSaidRemoved" => "removed {0}, {1} left",
|
||||
"ConsistSaidTurned" => "turned {0}",
|
||||
"ConsistSaidUnturned" => "unturned {0}",
|
||||
"ConsistSaidTurnedMany" => "turned {0}",
|
||||
"ConsistSaidMoved" => "moved {0} to {1}/{2}",
|
||||
"ConsistSaidReordered" => "reordered",
|
||||
"ConsistSaidReplaced" => "replaced {0} with {1}",
|
||||
"ConsistSaidChangedMany" => "changed {0}, {1} now",
|
||||
_ => throw new KeyNotFoundException(key)
|
||||
};
|
||||
|
||||
private static Unit[] Units(params string[] labels) =>
|
||||
labels.Select(l => new Unit(l.TrimEnd('^'), l.EndsWith('^'))).ToArray();
|
||||
|
||||
private static string? Say(string[] before, string[] after, int? vehicles = null) =>
|
||||
ConsistNarration.Describe(Units(before), Units(after), vehicles ?? after.Length, Loc);
|
||||
|
||||
[Fact]
|
||||
public void Nothing_changed_says_nothing() =>
|
||||
Assert.Null(Say(["EU07", "Bdhpumn"], ["EU07", "Bdhpumn"]));
|
||||
|
||||
[Fact]
|
||||
public void An_empty_consist_staying_empty_says_nothing() =>
|
||||
Assert.Null(Say([], []));
|
||||
|
||||
[Fact]
|
||||
public void Clearing_the_consist_says_it_is_empty() =>
|
||||
Assert.Equal("empty", Say(["EU07", "Bdhpumn"], []));
|
||||
|
||||
[Fact]
|
||||
public void A_vehicle_added_at_the_end_is_named_with_its_place() =>
|
||||
Assert.Equal("added Bdhpumn at 2/2", Say(["EU07"], ["EU07", "Bdhpumn"]));
|
||||
|
||||
[Fact]
|
||||
public void A_vehicle_added_in_the_middle_is_named_with_its_place() =>
|
||||
Assert.Equal("added WRmnouz at 2/3", Say(["EU07", "Bdhpumn"], ["EU07", "WRmnouz", "Bdhpumn"]));
|
||||
|
||||
[Fact]
|
||||
public void Two_vehicles_added_at_once_are_both_named() =>
|
||||
Assert.Equal("added A, B at 1/3", Say(["EU07"], ["A", "B", "EU07"]));
|
||||
|
||||
[Fact]
|
||||
public void A_removed_vehicle_is_named_with_the_count_left() =>
|
||||
Assert.Equal("removed WRmnouz, 2 left", Say(["EU07", "WRmnouz", "Bdhpumn"], ["EU07", "Bdhpumn"]));
|
||||
|
||||
[Fact]
|
||||
public void The_count_left_counts_vehicles_not_units() =>
|
||||
Assert.Equal("removed EU07, 3 left", Say(["EU07", "EN57"], ["EN57"], vehicles: 3));
|
||||
|
||||
[Fact]
|
||||
public void A_vehicle_turned_round_is_named() =>
|
||||
Assert.Equal("turned Bdhpumn", Say(["EU07", "Bdhpumn"], ["EU07", "Bdhpumn^"]));
|
||||
|
||||
[Fact]
|
||||
public void A_vehicle_turned_back_is_told_apart_from_one_turned() =>
|
||||
Assert.Equal("unturned Bdhpumn", Say(["EU07", "Bdhpumn^"], ["EU07", "Bdhpumn"]));
|
||||
|
||||
[Fact]
|
||||
public void Several_turned_at_once_are_counted() =>
|
||||
Assert.Equal("turned 3", Say(["A", "B", "C"], ["A^", "B^", "C^"]));
|
||||
|
||||
[Fact]
|
||||
public void A_vehicle_moved_towards_the_end_is_named_with_its_new_place() =>
|
||||
Assert.Equal("moved A to 3/4", Say(["A", "B", "C", "D"], ["B", "C", "A", "D"]));
|
||||
|
||||
[Fact]
|
||||
public void A_vehicle_moved_towards_the_front_is_named_with_its_new_place() =>
|
||||
Assert.Equal("moved C to 1/4", Say(["A", "B", "C", "D"], ["C", "A", "B", "D"]));
|
||||
|
||||
[Fact]
|
||||
public void Neighbours_swapped_are_told_as_one_moved() =>
|
||||
Assert.Equal("moved A to 2/2", Say(["A", "B"], ["B", "A"]));
|
||||
|
||||
[Fact]
|
||||
public void The_same_vehicles_shuffled_are_told_as_a_new_order() =>
|
||||
Assert.Equal("reordered", Say(["A", "B", "C", "D"], ["B", "A", "D", "C"]));
|
||||
|
||||
[Fact]
|
||||
public void One_vehicle_swapped_for_another_names_both() =>
|
||||
Assert.Equal("replaced B with X", Say(["A", "B", "C"], ["A", "X", "C"]));
|
||||
|
||||
[Fact]
|
||||
public void A_bigger_change_counts_the_vehicles_it_touched() =>
|
||||
Assert.Equal("changed 2, 3 now", Say(["A", "B", "C"], ["A", "X", "Y"]));
|
||||
|
||||
[Fact]
|
||||
public void Same_named_vehicles_next_to_each_other_still_count_one_added() =>
|
||||
Assert.Equal("added Bdhpumn at 3/3", Say(["EU07", "Bdhpumn"], ["EU07", "Bdhpumn", "Bdhpumn"]));
|
||||
}
|
||||
75
StarterNG.Tests/Controls.cs
Normal file
75
StarterNG.Tests/Controls.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Automation.Peers;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Themes.Fluent;
|
||||
using Avalonia.Threading;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
/// <summary>Keys, focus, layout and drawing for the tests of controls, with no one at the keyboard.</summary>
|
||||
internal static class Controls
|
||||
{
|
||||
/// <summary>Presses a key on the control as the keyboard would; true when the control took it.</summary>
|
||||
public static bool Press(Control control, Key key, KeyModifiers modifiers = KeyModifiers.None)
|
||||
{
|
||||
var e = new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = key, KeyModifiers = modifiers, Source = control };
|
||||
control.RaiseEvent(e);
|
||||
return e.Handled;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The control in a window of its own, shown, laid out and focused if it can be;
|
||||
/// themed, its parts are built from the templates the application uses.
|
||||
/// </summary>
|
||||
public static Window Show(Control control, double width = 640, double height = 720, bool themed = false)
|
||||
{
|
||||
HeadlessDrawing.Ensure();
|
||||
if (themed)
|
||||
Theme();
|
||||
var window = new Window { Width = width, Height = height, Content = control };
|
||||
window.Show();
|
||||
window.UpdateLayout();
|
||||
Settle();
|
||||
if (control.Focusable)
|
||||
control.Focus();
|
||||
return window;
|
||||
}
|
||||
|
||||
/// <summary>Lays the control out alone at the size given.</summary>
|
||||
public static void Lay(Control control, double width = 640, double height = 720)
|
||||
{
|
||||
HeadlessDrawing.Ensure();
|
||||
control.Measure(new Size(width, height));
|
||||
control.Arrange(new Rect(0, 0, width, height));
|
||||
}
|
||||
|
||||
/// <summary>Has the control draw itself once.</summary>
|
||||
public static void Draw(Control control)
|
||||
{
|
||||
HeadlessDrawing.Ensure();
|
||||
var group = new DrawingGroup();
|
||||
using var context = group.Open();
|
||||
control.Render(context);
|
||||
}
|
||||
|
||||
private static bool _themed;
|
||||
|
||||
// Where the application keeps it: given to a window alone, a slider or a number box was left without its parts.
|
||||
private static void Theme()
|
||||
{
|
||||
if (_themed)
|
||||
return;
|
||||
Avalonia.Application.Current!.Styles.Add(new FluentTheme());
|
||||
_themed = true;
|
||||
}
|
||||
|
||||
/// <summary>Runs what was left for the dispatcher to do later, such as naming a row once it is laid out.</summary>
|
||||
public static void Settle() => Dispatcher.UIThread.RunJobs();
|
||||
|
||||
public static AutomationPeer Peer(Control control) => ControlAutomationPeer.CreatePeerForElement(control);
|
||||
|
||||
public static RoutedEventArgs Click() => new(Button.ClickEvent);
|
||||
}
|
||||
376
StarterNG.Tests/DialogControlsTests.cs
Normal file
376
StarterNG.Tests/DialogControlsTests.cs
Normal file
@@ -0,0 +1,376 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Automation;
|
||||
using Avalonia.Automation.Peers;
|
||||
using Avalonia.Automation.Provider;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.VisualTree;
|
||||
using StarterNG.Controls;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Windows opened over another, searches and the starter's own controls as someone at the
|
||||
/// keyboard with a screen reader meets them, in windows built as the application builds them.
|
||||
/// </summary>
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class DialogControlsTests
|
||||
{
|
||||
static DialogControlsTests()
|
||||
{
|
||||
HeadlessDrawing.Ensure();
|
||||
DialogKeyboard.Install();
|
||||
ScreenReaderNames.Install();
|
||||
}
|
||||
|
||||
private static (Window Owner, Window Dialog) Dialog(Control content, bool escapeCloses = true)
|
||||
{
|
||||
var owner = new Window();
|
||||
owner.Show();
|
||||
var dialog = new Window { Width = 300, Height = 200, Content = content };
|
||||
DialogKeyboard.SetEscapeCloses(dialog, escapeCloses);
|
||||
_ = dialog.ShowDialog(owner);
|
||||
// Headless a window says it opened before it is given its owner; it says so again once it has one.
|
||||
Assert.Same(owner, dialog.Owner);
|
||||
dialog.RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Window.WindowOpenedEvent));
|
||||
Controls.Settle();
|
||||
return (owner, dialog);
|
||||
}
|
||||
|
||||
// --- windows over another ---
|
||||
|
||||
[Fact]
|
||||
public void A_dialog_opens_on_its_default_button_and_esc_closes_it()
|
||||
{
|
||||
var other = new Button { Content = "Nie" };
|
||||
var yes = new Button { Content = "Tak", IsDefault = true };
|
||||
var (owner, dialog) = Dialog(new StackPanel { Children = { other, yes } });
|
||||
bool closed = false;
|
||||
dialog.Closed += (_, _) => closed = true;
|
||||
|
||||
Assert.True(yes.IsFocused);
|
||||
Assert.False(Controls.Press(dialog, Key.Escape, KeyModifiers.Shift));
|
||||
Assert.True(Controls.Press(dialog, Key.Escape));
|
||||
Assert.True(closed);
|
||||
owner.Close();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Without_a_default_button_focus_goes_to_the_first_control()
|
||||
{
|
||||
var first = new TextBox();
|
||||
var (owner, dialog) = Dialog(new StackPanel { Children = { first, new Button { Content = "OK" } } });
|
||||
|
||||
Assert.True(first.IsFocused);
|
||||
dialog.Close();
|
||||
owner.Close();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_window_that_placed_the_focus_itself_keeps_it()
|
||||
{
|
||||
var first = new Button { Content = "1" };
|
||||
var chosen = new Button { Content = "2" };
|
||||
var (owner, dialog) = Dialog(new StackPanel { Children = { first, chosen } });
|
||||
chosen.Focus();
|
||||
Assert.False(first.IsFocused);
|
||||
|
||||
DialogKeyboard.FocusFirst(dialog);
|
||||
|
||||
Assert.True(chosen.IsFocused);
|
||||
dialog.Close();
|
||||
owner.Close();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Esc_does_not_close_a_window_that_stays_beside_the_main_one()
|
||||
{
|
||||
var (owner, dialog) = Dialog(new Button { Content = "OK" }, escapeCloses: false);
|
||||
bool closed = false;
|
||||
dialog.Closed += (_, _) => closed = true;
|
||||
|
||||
Assert.False(DialogKeyboard.GetEscapeCloses(dialog));
|
||||
Assert.False(Controls.Press(dialog, Key.Escape));
|
||||
Assert.False(closed);
|
||||
dialog.Close();
|
||||
owner.Close();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tab_with_only_the_window_focused_starts_from_the_first_control()
|
||||
{
|
||||
var first = new Button { Content = "1" };
|
||||
// Opened with nothing to focus, its controls put in afterwards.
|
||||
var (owner, dialog) = Dialog(new TextBlock { Text = "…" });
|
||||
dialog.Content = new StackPanel { Children = { first, new Button { Content = "2" } } };
|
||||
dialog.UpdateLayout();
|
||||
Assert.False(first.IsFocused);
|
||||
|
||||
Assert.True(Controls.Press(dialog, Key.Tab));
|
||||
Assert.True(first.IsFocused);
|
||||
dialog.Close();
|
||||
owner.Close();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_window_of_its_own_is_left_to_its_own_ways()
|
||||
{
|
||||
var button = new Button { Content = "1" };
|
||||
var window = Controls.Show(button);
|
||||
|
||||
Assert.False(Controls.Press(window, Key.Escape));
|
||||
window.Close();
|
||||
}
|
||||
|
||||
// --- searches ---
|
||||
|
||||
[Fact]
|
||||
public void A_search_says_how_many_entries_are_left_once_typing_pauses()
|
||||
{
|
||||
var box = new TextBox();
|
||||
var announcer = new Announcer { Child = box };
|
||||
var window = Controls.Show(announcer);
|
||||
try
|
||||
{
|
||||
var teller = new SearchResultsTeller(box, () => 3);
|
||||
box.Focus();
|
||||
box.Text = "EU07";
|
||||
teller.Changed();
|
||||
Thread.Sleep(SearchResultsTeller.Pause + TimeSpan.FromMilliseconds(300));
|
||||
Controls.Settle();
|
||||
|
||||
Assert.NotNull(announcer.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_empty_search_or_one_left_says_nothing()
|
||||
{
|
||||
var box = new TextBox();
|
||||
var elsewhere = new Button { Content = "x" };
|
||||
var announcer = new Announcer { Child = new StackPanel { Children = { box, elsewhere } } };
|
||||
var window = Controls.Show(announcer);
|
||||
try
|
||||
{
|
||||
var teller = new SearchResultsTeller(box, () => 3);
|
||||
box.Focus();
|
||||
box.Text = " ";
|
||||
teller.Changed();
|
||||
Thread.Sleep(SearchResultsTeller.Pause + TimeSpan.FromMilliseconds(300));
|
||||
Controls.Settle();
|
||||
Assert.Null(announcer.Message);
|
||||
|
||||
box.Text = "EU07";
|
||||
elsewhere.Focus();
|
||||
teller.Changed();
|
||||
Thread.Sleep(SearchResultsTeller.Pause + TimeSpan.FromMilliseconds(300));
|
||||
Controls.Settle();
|
||||
Assert.Null(announcer.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
// --- tree entries ---
|
||||
|
||||
[Fact]
|
||||
public void Every_part_of_a_row_tells_whether_the_entry_is_open()
|
||||
{
|
||||
var words = ExpandableTreeViewItem.StateWords;
|
||||
ExpandableTreeViewItem.StateWords = s => s switch
|
||||
{
|
||||
ExpandCollapseState.Expanded => "rozwinięte",
|
||||
ExpandCollapseState.Collapsed => "zwinięte",
|
||||
_ => null
|
||||
};
|
||||
var child = new ExpandableTreeViewItem { Header = "test32" };
|
||||
var group = new ExpandableTreeViewItem { Header = "Całkowo", Items = { child } };
|
||||
AutomationProperties.SetName(group, "Całkowo");
|
||||
var tree = new TreeView { Items = { group } };
|
||||
var window = Controls.Show(tree, themed: true);
|
||||
try
|
||||
{
|
||||
var row = group.GetVisualDescendants().OfType<Control>().First(c => c.Name == "PART_LayoutRoot");
|
||||
Assert.Equal("zwinięte", AutomationProperties.GetHelpText(row));
|
||||
Assert.Equal("Całkowo", AutomationProperties.GetName(row));
|
||||
|
||||
var peer = Controls.Peer(group);
|
||||
((IExpandCollapseProvider)peer).Expand();
|
||||
Controls.Settle();
|
||||
Assert.Equal("rozwinięte", AutomationProperties.GetHelpText(row));
|
||||
|
||||
AutomationProperties.SetName(group, "Całkowo i okolice");
|
||||
Assert.Equal("Całkowo i okolice", AutomationProperties.GetName(row));
|
||||
|
||||
group.Header = "Całkowo 2";
|
||||
Controls.Settle();
|
||||
window.UpdateLayout();
|
||||
Assert.Same(group, child.ReadableParent);
|
||||
Assert.Same(peer, Controls.Peer(child).GetParent());
|
||||
|
||||
var leafRow = child.GetVisualDescendants().OfType<Control>().First(c => c.Name == "PART_LayoutRoot");
|
||||
Assert.Null(AutomationProperties.GetHelpText(leafRow));
|
||||
|
||||
tree.Items.Clear();
|
||||
Controls.Settle();
|
||||
}
|
||||
finally
|
||||
{
|
||||
ExpandableTreeViewItem.StateWords = words;
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
// --- sliders and number boxes ---
|
||||
|
||||
[Fact]
|
||||
public void The_track_and_knob_of_a_slider_are_named_with_its_value()
|
||||
{
|
||||
var fog = new ReadableSlider { Minimum = 0, Maximum = 1000, Value = 500, ValueText = "2,4 km" };
|
||||
AutomationProperties.SetName(fog, "Widoczność");
|
||||
var window = Controls.Show(fog, themed: true);
|
||||
try
|
||||
{
|
||||
var track = fog.GetVisualDescendants().OfType<Avalonia.Controls.Primitives.Track>().Single();
|
||||
Assert.Equal("Widoczność: 2,4 km", AutomationProperties.GetName(track));
|
||||
|
||||
fog.ValueText = "3 km";
|
||||
Assert.Equal("Widoczność: 3 km", AutomationProperties.GetName(track));
|
||||
|
||||
var value = Assert.IsAssignableFrom<IValueProvider>(Controls.Peer(fog));
|
||||
Assert.False(value.IsReadOnly);
|
||||
Assert.Equal("Slider", Controls.Peer(fog).GetClassName());
|
||||
Assert.Throws<ArgumentException>(() => value.SetValue("duĹĽo"));
|
||||
}
|
||||
finally
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_number_box_gives_its_name_to_the_text_inside_and_its_arrows_say_what_they_do()
|
||||
{
|
||||
var box = new NumericUpDown { Value = 3 };
|
||||
AutomationProperties.SetName(box, "Liczba wagonĂłw");
|
||||
var window = Controls.Show(box, themed: true);
|
||||
try
|
||||
{
|
||||
var text = box.GetVisualDescendants().OfType<TextBox>().Single();
|
||||
Assert.Equal("Liczba wagonĂłw", AutomationProperties.GetName(text));
|
||||
|
||||
var arrows = box.GetVisualDescendants().OfType<Control>()
|
||||
.Where(c => c.Name is "PART_IncreaseButton" or "PART_DecreaseButton").ToList();
|
||||
Assert.NotEmpty(arrows);
|
||||
Assert.All(arrows, a => Assert.False(KeyboardNavigation.GetIsTabStop(a)));
|
||||
Assert.All(arrows, a => Assert.False(string.IsNullOrEmpty(AutomationProperties.GetName(a))));
|
||||
}
|
||||
finally
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_picker_is_reached_through_its_button_named_with_the_label_and_value()
|
||||
{
|
||||
var date = new DatePicker { SelectedDate = new DateTimeOffset(2026, 11, 15, 0, 0, 0, TimeSpan.Zero) };
|
||||
var time = new TimePicker { SelectedTime = new TimeSpan(7, 5, 0) };
|
||||
AutomationProperties.SetName(date, "Data");
|
||||
AutomationProperties.SetName(time, "Godzina");
|
||||
var window = Controls.Show(new StackPanel { Children = { date, time } }, themed: true);
|
||||
try
|
||||
{
|
||||
var dateButton = date.GetVisualDescendants().OfType<Button>().First(b => b.Name == "PART_FlyoutButton");
|
||||
var timeButton = time.GetVisualDescendants().OfType<Button>().First(b => b.Name == "PART_FlyoutButton");
|
||||
Assert.StartsWith("Data", AutomationProperties.GetName(dateButton));
|
||||
Assert.StartsWith("Godzina", AutomationProperties.GetName(timeButton));
|
||||
|
||||
time.SelectedTime = new TimeSpan(8, 0, 0);
|
||||
Assert.StartsWith("Godzina", AutomationProperties.GetName(timeButton));
|
||||
}
|
||||
finally
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_list_says_its_new_choice_when_the_arrows_change_it()
|
||||
{
|
||||
var combo = new ComboBox { ItemsSource = new[] { "Niska", "Ĺšrednia", "Wysoka" }, SelectedIndex = 0 };
|
||||
var announcer = new Announcer { Child = combo };
|
||||
var window = Controls.Show(announcer, themed: true);
|
||||
try
|
||||
{
|
||||
combo.Focus();
|
||||
combo.SelectedIndex = 2;
|
||||
|
||||
Assert.Equal("Wysoka", announcer.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
// --- settings sections ---
|
||||
|
||||
private static SettingsSection Section() => new()
|
||||
{
|
||||
Template = new FuncControlTemplate<SettingsSection>((_, scope) =>
|
||||
{
|
||||
var reset = new Button { Name = "PART_Reset" };
|
||||
scope.Register(reset.Name, reset);
|
||||
return reset;
|
||||
})
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void A_sections_reset_button_says_which_section_it_puts_back()
|
||||
{
|
||||
var section = Section();
|
||||
section.Header = "Grafika";
|
||||
section.ResetLabel = "Przywróć";
|
||||
SettingsSection? asked = null;
|
||||
section.ResetRequested += s => asked = s;
|
||||
Controls.Lay(section);
|
||||
section.ApplyTemplate();
|
||||
var reset = (Button)section.GetVisualChildren().Single();
|
||||
|
||||
Assert.Equal("Przywróć: Grafika", AutomationProperties.GetName(reset));
|
||||
section.Header = "Dźwięk";
|
||||
Assert.Equal("Przywróć: Dźwięk", AutomationProperties.GetName(reset));
|
||||
|
||||
section.ResetLabel = null;
|
||||
section.Header = null;
|
||||
Assert.True(string.IsNullOrEmpty(AutomationProperties.GetName(reset)));
|
||||
|
||||
reset.RaiseEvent(Controls.Click());
|
||||
Assert.Same(section, asked);
|
||||
|
||||
section.Template = Section().Template;
|
||||
section.ApplyTemplate();
|
||||
Assert.Null(section.ToggleKey);
|
||||
Assert.Null(section.Toggle);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_sections_list_of_levels_is_named_as_the_level_of_that_section()
|
||||
{
|
||||
var level = new ComboBox();
|
||||
var section = new SettingsSection { Level = level, Header = "Grafika", ToggleKey = "shadows", Toggle = new CheckBox() };
|
||||
|
||||
Assert.EndsWith("Grafika", AutomationProperties.GetName(level));
|
||||
Assert.Same(level, section.Level);
|
||||
Assert.Equal("shadows", section.ToggleKey);
|
||||
}
|
||||
}
|
||||
53
StarterNG.Tests/ExecutableListTests.cs
Normal file
53
StarterNG.Tests/ExecutableListTests.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using StarterNG.Views;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
public class ExecutableListTests
|
||||
{
|
||||
[Fact]
|
||||
public void A_build_copied_in_joins_the_list_and_the_choice_stays()
|
||||
{
|
||||
var (entries, chosen) = ExecutableList.Refreshed(["eu07_new.exe", "eu07.exe"], "eu07.exe");
|
||||
|
||||
Assert.Equal(new[] { "eu07_new.exe", "eu07.exe" }, entries);
|
||||
Assert.Equal(1, chosen);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Automatic_stays_automatic()
|
||||
{
|
||||
var (entries, chosen) = ExecutableList.Refreshed(["eu07_new.exe", "eu07.exe"], null);
|
||||
|
||||
Assert.Equal(new[] { "eu07_new.exe", "eu07.exe" }, entries);
|
||||
Assert.Equal(-1, chosen);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_simulator_picked_from_elsewhere_stays_on_the_list_and_chosen()
|
||||
{
|
||||
const string picked = @"D:\build\eu07.exe";
|
||||
|
||||
var (entries, chosen) = ExecutableList.Refreshed(["eu07.exe"], picked);
|
||||
|
||||
Assert.Equal(new[] { "eu07.exe", picked }, entries);
|
||||
Assert.Equal(1, chosen);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_chosen_file_since_removed_stays_chosen_rather_than_switch_silently()
|
||||
{
|
||||
var (entries, chosen) = ExecutableList.Refreshed(["eu07.exe"], "eu07_old.exe");
|
||||
|
||||
Assert.Equal(new[] { "eu07.exe", "eu07_old.exe" }, entries);
|
||||
Assert.Equal(1, chosen);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_choice_is_found_whatever_the_case_of_its_name()
|
||||
{
|
||||
var (entries, chosen) = ExecutableList.Refreshed(["EU07.exe"], "eu07.exe");
|
||||
|
||||
Assert.Equal(new[] { "EU07.exe" }, entries);
|
||||
Assert.Equal(0, chosen);
|
||||
}
|
||||
}
|
||||
82
StarterNG.Tests/ExecutableNoAvx2Tests.cs
Normal file
82
StarterNG.Tests/ExecutableNoAvx2Tests.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using StarterNG.Domain.Settings;
|
||||
using StarterNG.Infrastructure.Settings;
|
||||
using StarterNG.Tests.Fakes;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
public class ExecutableNoAvx2Tests
|
||||
{
|
||||
private const string Elf = "\u007fELF binary";
|
||||
private static readonly DateTime Older = new(2026, 11, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
private static readonly DateTime Newer = new(2026, 11, 15, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
private static InMemoryFileSystem With(params string[] names)
|
||||
{
|
||||
var files = new InMemoryFileSystem();
|
||||
foreach (string name in names)
|
||||
files.WithExecutable(TestInstallation.At(name), Elf);
|
||||
return files;
|
||||
}
|
||||
|
||||
private static string Automatic(InMemoryFileSystem files, bool lacksAvx2) =>
|
||||
Path.GetFileName(new TestInstallation(files, lacksAvx2: lacksAvx2).Executables
|
||||
.Resolve(new SimulatorSettings(), out _));
|
||||
|
||||
[Theory]
|
||||
[InlineData("eu07_noavx2", true)]
|
||||
[InlineData("eu07-x64_2026-11-15_abc1234_noavx2.exe", true)]
|
||||
[InlineData("EU07_NOAVX2.EXE", true)]
|
||||
[InlineData("eu07", false)]
|
||||
[InlineData("eu07-x64_2026-11-15_abc1234.exe", false)]
|
||||
[InlineData("eu07_noavx2_old.exe", false)]
|
||||
public void A_build_without_avx2_is_known_by_the_end_of_its_name(string name, bool noAvx2) =>
|
||||
Assert.Equal(noAvx2, ExecutableLocator.IsNoAvx2(name));
|
||||
|
||||
[Fact]
|
||||
public void Without_avx2_the_build_without_it_is_chosen_over_the_canonical_one() =>
|
||||
Assert.Equal("eu07_noavx2", Automatic(With("eu07", "eu07_noavx2"), lacksAvx2: true));
|
||||
|
||||
[Fact]
|
||||
public void With_avx2_the_build_made_for_it_stays_first() =>
|
||||
Assert.Equal("eu07", Automatic(With("eu07", "eu07_noavx2"), lacksAvx2: false));
|
||||
|
||||
[Fact]
|
||||
public void With_avx2_a_build_without_it_is_used_when_it_is_the_only_one() =>
|
||||
Assert.Equal("eu07_noavx2", Automatic(With("eu07_noavx2"), lacksAvx2: false));
|
||||
|
||||
[Fact]
|
||||
public void Without_avx2_the_newest_build_without_it_is_chosen()
|
||||
{
|
||||
var files = With("eu07_2026-11-01_aaa_noavx2", "eu07_2026-11-15_bbb_noavx2")
|
||||
.WrittenAt(TestInstallation.At("eu07_2026-11-01_aaa_noavx2"), Older)
|
||||
.WrittenAt(TestInstallation.At("eu07_2026-11-15_bbb_noavx2"), Newer);
|
||||
|
||||
Assert.Equal("eu07_2026-11-15_bbb_noavx2", Automatic(files, lacksAvx2: true));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Without_avx2_and_no_build_without_it_the_usual_choice_stands() =>
|
||||
Assert.Equal("eu07", Automatic(With("eu07-dev", "eu07"), lacksAvx2: true));
|
||||
|
||||
[Fact]
|
||||
public void Only_a_build_with_avx2_on_a_processor_without_it_will_not_run()
|
||||
{
|
||||
var lacking = new TestInstallation(lacksAvx2: true).Executables;
|
||||
var able = new TestInstallation(lacksAvx2: false).Executables;
|
||||
|
||||
Assert.True(lacking.WillNotRunHere("eu07.exe"));
|
||||
Assert.False(lacking.WillNotRunHere("eu07-x64_noavx2.exe"));
|
||||
Assert.False(able.WillNotRunHere("eu07.exe"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_newest_build_without_avx2_is_offered_by_name()
|
||||
{
|
||||
var files = With("eu07", "eu07_2026-11-01_aaa_noavx2", "eu07_2026-11-15_bbb_noavx2")
|
||||
.WrittenAt(TestInstallation.At("eu07_2026-11-01_aaa_noavx2"), Older)
|
||||
.WrittenAt(TestInstallation.At("eu07_2026-11-15_bbb_noavx2"), Newer);
|
||||
|
||||
Assert.Equal("eu07_2026-11-15_bbb_noavx2", new TestInstallation(files).Executables.NewestNoAvx2());
|
||||
Assert.Null(new TestInstallation(With("eu07")).Executables.NewestNoAvx2());
|
||||
}
|
||||
}
|
||||
63
StarterNG.Tests/ExpandableTreeViewItemTests.cs
Normal file
63
StarterNG.Tests/ExpandableTreeViewItemTests.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using Avalonia.Automation;
|
||||
using Avalonia.Automation.Peers;
|
||||
using Avalonia.Automation.Provider;
|
||||
using StarterNG.Controls;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class ExpandableTreeViewItemTests
|
||||
{
|
||||
private static IExpandCollapseProvider Peer(ExpandableTreeViewItem item) =>
|
||||
Assert.IsAssignableFrom<IExpandCollapseProvider>(ControlAutomationPeer.CreatePeerForElement(item));
|
||||
|
||||
[Fact]
|
||||
public void A_scenery_with_nothing_under_it_is_a_leaf()
|
||||
{
|
||||
var peer = Peer(new ExpandableTreeViewItem { Header = "test32" });
|
||||
|
||||
Assert.Equal(ExpandCollapseState.LeafNode, peer.ExpandCollapseState);
|
||||
Assert.False(peer.ShowsMenu);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_group_says_whether_it_is_open()
|
||||
{
|
||||
Assert.Equal(ExpandCollapseState.Collapsed, ExpandableTreeViewItem.StateFor(hasChildren: true, expanded: false));
|
||||
Assert.Equal(ExpandCollapseState.Expanded, ExpandableTreeViewItem.StateFor(hasChildren: true, expanded: true));
|
||||
Assert.Equal(ExpandCollapseState.LeafNode, ExpandableTreeViewItem.StateFor(hasChildren: false, expanded: true));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_screen_reader_can_open_and_close_an_entry()
|
||||
{
|
||||
var item = new ExpandableTreeViewItem { Header = "Całkowo" };
|
||||
var peer = Peer(item);
|
||||
|
||||
peer.Expand();
|
||||
Assert.True(item.IsExpanded);
|
||||
|
||||
peer.Collapse();
|
||||
Assert.False(item.IsExpanded);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_entry_on_its_own_has_nothing_over_it()
|
||||
{
|
||||
var item = new ExpandableTreeViewItem { Header = "Całkowo", IsExpanded = true };
|
||||
var peer = ControlAutomationPeer.CreatePeerForElement(item);
|
||||
|
||||
Assert.Null(item.ReadableParent);
|
||||
Assert.Null(peer.GetParent());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_row_under_the_mouse_is_told_open_or_closed_and_a_leaf_nothing()
|
||||
{
|
||||
static string Loc(string key) => key == "TreeExpanded" ? "rozwinięte" : "zwinięte";
|
||||
|
||||
Assert.Equal("rozwinięte", ExpandableTreeViewItem.WordFor(ExpandCollapseState.Expanded, Loc));
|
||||
Assert.Equal("zwinięte", ExpandableTreeViewItem.WordFor(ExpandCollapseState.Collapsed, Loc));
|
||||
Assert.Null(ExpandableTreeViewItem.WordFor(ExpandCollapseState.LeafNode, Loc));
|
||||
}
|
||||
}
|
||||
@@ -97,6 +97,8 @@ public sealed class InMemoryFileSystem : IFileSystem
|
||||
return this;
|
||||
}
|
||||
|
||||
public bool IgnoresCase => _comparison == StringComparison.OrdinalIgnoreCase;
|
||||
|
||||
public bool FileExists(string path) => _files.ContainsKey(Normalize(path));
|
||||
|
||||
/// <summary>A link counts as the directory it points at, as the disk has it.</summary>
|
||||
@@ -273,6 +275,9 @@ public sealed class InMemoryFileSystem : IFileSystem
|
||||
return;
|
||||
slash = key.LastIndexOf('/');
|
||||
}
|
||||
// An absolute Unix path's parent is the root, "/", which normalises to "".
|
||||
if (slash == 0)
|
||||
_directories.Add("");
|
||||
}
|
||||
|
||||
private static string Normalize(string path) =>
|
||||
|
||||
@@ -14,7 +14,7 @@ public sealed class TestInstallation
|
||||
{
|
||||
public const string Root = "game";
|
||||
|
||||
public TestInstallation(InMemoryFileSystem? files = null, DateTime? now = null)
|
||||
public TestInstallation(InMemoryFileSystem? files = null, DateTime? now = null, bool lacksAvx2 = false)
|
||||
{
|
||||
Files = files ?? new InMemoryFileSystem();
|
||||
Paths = new GamePaths(Root);
|
||||
@@ -35,7 +35,8 @@ public sealed class TestInstallation
|
||||
SettingsPaths.VehicleCacheDirectory());
|
||||
Library = new GameLibrary(VehicleCache, Sceneries, MiniTextures, Physics, Log);
|
||||
|
||||
Executables = new ExecutableLocator(Files, Paths, Environment, Log);
|
||||
// The processor of the machine running the tests is not the one tested.
|
||||
Executables = new ExecutableLocator(Files, Paths, Environment, Log, lacksAvx2);
|
||||
SettingsStore = new SettingsStore(Files, Clock, Log, SettingsPaths, new SettingsSerializer(), Executables);
|
||||
Tapes = new HaslerTapes(Files, Paths, Log, Clock, new TimetableLocator(Files), () => SimulatorRunning);
|
||||
}
|
||||
|
||||
98
StarterNG.Tests/FieldLabelTests.cs
Normal file
98
StarterNG.Tests/FieldLabelTests.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using Avalonia.Automation;
|
||||
using Avalonia.Controls;
|
||||
using StarterNG.Controls;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class FieldLabelTests
|
||||
{
|
||||
static FieldLabelTests() => ScreenReaderNames.Install();
|
||||
|
||||
private static T At<T>(T control, int row, int column) where T : Control
|
||||
{
|
||||
Grid.SetRow(control, row);
|
||||
Grid.SetColumn(control, column);
|
||||
return control;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_field_is_named_by_the_nearest_label_left_of_it_in_its_row()
|
||||
{
|
||||
var far = At(new TextBlock { Text = "Pogoda" }, 0, 0);
|
||||
var near = At(new TextBlock { Text = "Temperatura" }, 0, 1);
|
||||
var value = At(new TextBlock { Text = "-5 °C" }, 0, 3);
|
||||
var other = At(new TextBlock { Text = "Widoczność" }, 1, 1);
|
||||
var slider = At(new Slider(), 0, 2);
|
||||
_ = new Grid { Children = { far, near, value, other, slider } };
|
||||
|
||||
ScreenReaderNames.NameByLabel(slider);
|
||||
|
||||
Assert.Equal("Temperatura", AutomationProperties.GetName(slider));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_field_with_nothing_to_its_left_is_named_by_the_label_above()
|
||||
{
|
||||
var label = At(new TextBlock { Text = "Klasa" }, 0, 1);
|
||||
var combo = At(new ComboBox(), 1, 1);
|
||||
_ = new Grid { Children = { label, combo } };
|
||||
|
||||
Assert.Same(label, ScreenReaderNames.LabelOf(combo));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void In_a_panel_the_label_is_the_text_just_before_the_field()
|
||||
{
|
||||
var label = new TextBlock { Text = "Profil" };
|
||||
var box = new TextBox();
|
||||
var after = new TextBox();
|
||||
_ = new StackPanel { Children = { label, box, new Button(), after } };
|
||||
|
||||
ScreenReaderNames.NameByLabel(box);
|
||||
|
||||
Assert.Equal("Profil", AutomationProperties.GetName(box));
|
||||
Assert.Null(ScreenReaderNames.LabelOf(after));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_hidden_text_or_a_long_description_is_no_label()
|
||||
{
|
||||
var hidden = new TextBlock { Text = "Ukryte", IsVisible = false };
|
||||
var first = new ComboBox();
|
||||
var description = new TextBlock { Text = new string('x', 61) };
|
||||
var second = new ComboBox();
|
||||
_ = new StackPanel { Children = { hidden, first, description, second } };
|
||||
|
||||
Assert.Null(ScreenReaderNames.LabelOf(first));
|
||||
Assert.Null(ScreenReaderNames.LabelOf(second));
|
||||
Assert.Null(ScreenReaderNames.LabelOf(new ComboBox()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_name_follows_the_label_into_another_language()
|
||||
{
|
||||
var label = new TextBlock { Text = "Data" };
|
||||
var picker = new DatePicker();
|
||||
_ = new DockPanel { Children = { label, picker } };
|
||||
|
||||
ScreenReaderNames.NameByLabel(picker);
|
||||
ScreenReaderNames.NameByLabel(picker);
|
||||
label.Text = "Date";
|
||||
|
||||
Assert.Equal("Date", AutomationProperties.GetName(picker));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_name_given_on_purpose_beats_the_label()
|
||||
{
|
||||
var label = new TextBlock { Text = "Szukaj" };
|
||||
var box = new TextBox();
|
||||
AutomationProperties.SetName(box, "Szukaj pojazdów");
|
||||
_ = new StackPanel { Children = { label, box } };
|
||||
|
||||
ScreenReaderNames.NameByLabel(box);
|
||||
|
||||
Assert.Equal("Szukaj pojazdów", AutomationProperties.GetName(box));
|
||||
}
|
||||
}
|
||||
27
StarterNG.Tests/HeadlessDrawing.cs
Normal file
27
StarterNG.Tests/HeadlessDrawing.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Headless;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Text and drawing for tests of controls that draw themselves: the headless platform
|
||||
/// with its stand-in fonts, set up once, on the thread the control tests run on.
|
||||
/// </summary>
|
||||
public static class HeadlessDrawing
|
||||
{
|
||||
private static readonly object Gate = new();
|
||||
private static bool _ready;
|
||||
|
||||
public static void Ensure()
|
||||
{
|
||||
lock (Gate)
|
||||
{
|
||||
if (_ready)
|
||||
return;
|
||||
AppBuilder.Configure<Avalonia.Application>()
|
||||
.UseHeadless(new AvaloniaHeadlessPlatformOptions { UseHeadlessDrawing = true })
|
||||
.SetupWithoutStarting();
|
||||
_ready = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
105
StarterNG.Tests/KeyBindingsTabTests.cs
Normal file
105
StarterNG.Tests/KeyBindingsTabTests.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using Avalonia.Automation;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.VisualTree;
|
||||
using StarterNG.Application;
|
||||
using StarterNG.Classes;
|
||||
using StarterNG.Controls;
|
||||
using StarterNG.Tests.Fakes;
|
||||
using StarterNG.Views;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
/// <summary>The key bindings as someone at the keyboard binds a key: the list of commands, and the keyboard drawn beside it walked with the arrows.</summary>
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class KeyBindingsTabTests
|
||||
{
|
||||
static KeyBindingsTabTests()
|
||||
{
|
||||
if (AppServices.CurrentOrNull is null)
|
||||
{
|
||||
var installation = new TestInstallation(new InMemoryFileSystem());
|
||||
AppServices.Initialize(installation.Paths, installation.Files, installation.Clock, installation.Environment);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed record Shown(Window Window, KeyBindingsTab Tab, Grid Host, Announcer Announcer) : IDisposable
|
||||
{
|
||||
public List<Border> Caps() =>
|
||||
Host.GetVisualDescendants().OfType<Border>().Where(b => b.Classes.Contains("KeyCap")).ToList();
|
||||
|
||||
public void Dispose() => Window.Close();
|
||||
}
|
||||
|
||||
private static Shown Open()
|
||||
{
|
||||
KeyboardConfig.Instance.Parse("hornhighactivate a\nhornlowactivate none\n");
|
||||
var host = new Grid();
|
||||
var search = new TextBox();
|
||||
var tab = new KeyBindingsTab(host, search);
|
||||
var announcer = new Announcer { Child = new DockPanel { Children = { search, host } } };
|
||||
var window = Controls.Show(announcer, 1600, 1000, themed: true);
|
||||
window.AddHandler(InputElement.KeyDownEvent, tab.OnKeyDown, Avalonia.Interactivity.RoutingStrategies.Tunnel);
|
||||
tab.Build();
|
||||
window.UpdateLayout();
|
||||
Controls.Settle();
|
||||
return new Shown(window, tab, host, announcer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_arrows_walk_the_keys_as_they_lie()
|
||||
{
|
||||
using var shown = Open();
|
||||
var caps = shown.Caps();
|
||||
Assert.True(caps.Count > 50);
|
||||
Assert.All(caps, c => Assert.False(string.IsNullOrEmpty(AutomationProperties.GetName(c))));
|
||||
|
||||
caps[0].Focus();
|
||||
Assert.True(Controls.Press(caps[0], Key.Right));
|
||||
Assert.True(caps[1].IsFocused);
|
||||
Assert.True(Controls.Press(caps[1], Key.Left));
|
||||
Assert.True(caps[0].IsFocused);
|
||||
Assert.True(Controls.Press(caps[0], Key.Left));
|
||||
Assert.True(caps[0].IsFocused);
|
||||
|
||||
Assert.True(Controls.Press(caps[0], Key.End));
|
||||
Assert.True(caps[^1].IsFocused);
|
||||
Assert.True(Controls.Press(caps[^1], Key.Right));
|
||||
Assert.True(caps[^1].IsFocused);
|
||||
Assert.True(Controls.Press(caps[^1], Key.Home));
|
||||
Assert.True(caps[0].IsFocused);
|
||||
|
||||
Assert.True(Controls.Press(caps[0], Key.Down));
|
||||
var below = caps.Single(c => c.IsFocused);
|
||||
Assert.NotSame(caps[0], below);
|
||||
Assert.True(Controls.Press(below, Key.Up));
|
||||
var above = caps.Single(c => c.IsFocused);
|
||||
Assert.NotSame(below, above);
|
||||
Assert.True(Controls.Press(above, Key.Up));
|
||||
Assert.True(above.IsFocused);
|
||||
|
||||
Assert.False(Controls.Press(caps[0], Key.F24));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_binding_takes_the_next_key_pressed_and_esc_gives_up_waiting()
|
||||
{
|
||||
using var shown = Open();
|
||||
Assert.True(shown.Tab.ShownCount >= 2);
|
||||
var combos = shown.Host.GetVisualDescendants().OfType<Button>()
|
||||
.Where(b => b.Classes.Contains("Flat") && b.MinWidth == 140).ToList();
|
||||
Assert.NotEmpty(combos);
|
||||
|
||||
combos[0].RaiseEvent(Controls.Click());
|
||||
Assert.True(shown.Tab.IsCapturing);
|
||||
Assert.True(Controls.Press(combos[0], Key.Escape));
|
||||
Assert.False(shown.Tab.IsCapturing);
|
||||
|
||||
combos[0].RaiseEvent(Controls.Click());
|
||||
Assert.False(Controls.Press(combos[0], Key.LeftShift));
|
||||
Assert.True(shown.Tab.IsCapturing);
|
||||
Assert.True(Controls.Press(combos[0], Key.J, KeyModifiers.Control | KeyModifiers.Shift));
|
||||
Assert.False(shown.Tab.IsCapturing);
|
||||
Assert.True(KeyboardConfig.Instance.Dirty);
|
||||
}
|
||||
}
|
||||
40
StarterNG.Tests/KeyboardFocusTests.cs
Normal file
40
StarterNG.Tests/KeyboardFocusTests.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using StarterNG.Controls;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class KeyboardFocusTests
|
||||
{
|
||||
private readonly Button _window = new();
|
||||
private readonly Button _first = new();
|
||||
private readonly Button _last = new();
|
||||
|
||||
private IInputElement? Tab(Key key, KeyModifiers modifiers, IInputElement? focused) =>
|
||||
KeyboardFocus.TabFromNowhere(key, modifiers, focused, _window, () => _first, () => _last);
|
||||
|
||||
[Fact]
|
||||
public void Tab_with_nothing_focused_starts_from_the_first_control()
|
||||
{
|
||||
Assert.Same(_first, Tab(Key.Tab, KeyModifiers.None, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Shift_tab_with_only_the_window_focused_starts_from_the_last_control()
|
||||
{
|
||||
Assert.Same(_last, Tab(Key.Tab, KeyModifiers.Shift, _window));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tab_from_a_focused_control_is_left_to_the_usual_order()
|
||||
{
|
||||
Assert.Null(Tab(Key.Tab, KeyModifiers.None, new TextBox()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Other_keys_are_left_alone()
|
||||
{
|
||||
Assert.Null(Tab(Key.Enter, KeyModifiers.None, null));
|
||||
}
|
||||
}
|
||||
28
StarterNG.Tests/LoadingScreenLogoTests.cs
Normal file
28
StarterNG.Tests/LoadingScreenLogoTests.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using StarterNG.Infrastructure;
|
||||
using StarterNG.Tests.Fakes;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
public class LoadingScreenLogoTests
|
||||
{
|
||||
private static InMemoryFileSystem Logos() => new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile("game/Textures/Logo/EP09_Logo.PNG", "png")
|
||||
.WithFile("game/Textures/Logo/Braniewo.JPG", "jpg")
|
||||
.WithFile("game/Textures/Logo/Logo1.BMP", "bmp")
|
||||
.WithFile("game/Textures/Logo/notes.txt", "");
|
||||
|
||||
[Theory]
|
||||
[InlineData("ep09_logo", null, "EP09_Logo.PNG")]
|
||||
[InlineData("ep09_logo.bmp", "braniewo", "EP09_Logo.PNG")]
|
||||
[InlineData(null, "braniewo", "Braniewo.JPG")]
|
||||
[InlineData("brak", "brak", "Logo1.BMP")]
|
||||
public void The_logo_is_found_whatever_the_case_of_its_name_and_extension(string? logo, string? scenery,
|
||||
string onDisk) =>
|
||||
Assert.Equal(Path.GetFullPath(Path.Combine("game", "Textures", "Logo", onDisk)),
|
||||
Path.GetFullPath(LoadingScreen.FindSource(Logos(), "game", logo, scenery, new Random(1))!));
|
||||
|
||||
[Fact]
|
||||
public void With_no_logo_folder_there_is_no_logo() =>
|
||||
Assert.Null(LoadingScreen.FindSource(new InMemoryFileSystem(caseSensitive: true), "game", "x", "y",
|
||||
new Random(1)));
|
||||
}
|
||||
102
StarterNG.Tests/MouseReadingTests.cs
Normal file
102
StarterNG.Tests/MouseReadingTests.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using Avalonia.Automation;
|
||||
using Avalonia.Controls;
|
||||
using StarterNG.Controls;
|
||||
using StarterNG.Infrastructure;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class MouseReadingTests
|
||||
{
|
||||
static MouseReadingTests() => ScreenReaderNames.Install();
|
||||
|
||||
[Fact]
|
||||
public void A_picture_on_a_button_is_read_as_the_button()
|
||||
{
|
||||
var logo = new Image();
|
||||
var button = new Button { Content = logo };
|
||||
|
||||
ScreenReaderNames.NamePicture(logo);
|
||||
|
||||
Assert.Same(button, AutomationProperties.GetLabeledBy(logo));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_picture_beside_a_caption_is_read_as_the_caption()
|
||||
{
|
||||
var mini = new Image();
|
||||
var caption = new TextBlock { Text = "EU07" };
|
||||
_ = new StackPanel { Children = { mini, new TextBlock { IsVisible = false, Text = "ukryte" }, caption } };
|
||||
|
||||
ScreenReaderNames.NamePicture(mini);
|
||||
|
||||
Assert.Same(caption, AutomationProperties.GetLabeledBy(mini));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_picture_in_a_card_is_read_as_the_card_tooltip()
|
||||
{
|
||||
var mini = new Image();
|
||||
var card = new Border { Child = new Panel { Children = { mini } } };
|
||||
ToolTip.SetTip(card, "EP09-018");
|
||||
|
||||
ScreenReaderNames.NamePicture(mini);
|
||||
|
||||
Assert.Equal("EP09-018", AutomationProperties.GetName(mini));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_picture_named_on_purpose_or_far_from_any_name_is_left_alone()
|
||||
{
|
||||
var named = new Image();
|
||||
AutomationProperties.SetName(named, "Obraz scenerii: baltyk");
|
||||
var far = new Image();
|
||||
var outer = new Border { Child = new Border { Child = new Border { Child = new Border { Child = far } } } };
|
||||
ToolTip.SetTip(outer, "Opis");
|
||||
_ = new Button { Content = named };
|
||||
|
||||
ScreenReaderNames.NamePicture(named);
|
||||
ScreenReaderNames.NamePicture(far);
|
||||
|
||||
Assert.Equal("Obraz scenerii: baltyk", AutomationProperties.GetName(named));
|
||||
Assert.Null(AutomationProperties.GetLabeledBy(named));
|
||||
Assert.Null(AutomationProperties.GetName(far));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_list_entry_is_read_as_its_text()
|
||||
{
|
||||
var named = new ComboBoxItem { Content = "Lato" };
|
||||
AutomationProperties.SetName(named, "Pora: lato");
|
||||
|
||||
Assert.Equal("Pogodnie", ScreenReaderNames.SpokenItem(new ComboBoxItem { Content = "Pogodnie" }));
|
||||
Assert.Equal("Pora: lato", ScreenReaderNames.SpokenItem(named));
|
||||
Assert.Equal("12", ScreenReaderNames.SpokenItem(12));
|
||||
Assert.Null(ScreenReaderNames.SpokenItem(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_list_not_in_focus_says_nothing_of_its_new_choice()
|
||||
{
|
||||
var combo = new ComboBox();
|
||||
var announcer = new Announcer();
|
||||
announcer.Child = combo;
|
||||
|
||||
ScreenReaderNames.TellChoice(combo);
|
||||
|
||||
Assert.Null(announcer.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_figure_says_its_label_and_its_value_together()
|
||||
{
|
||||
var host = new StackPanel();
|
||||
|
||||
StatsBar.Fill(host, new[] { ("Masa [t]:", "440"), ("Tor:", "tor0325") });
|
||||
|
||||
var figure = Assert.IsType<TextBlock>(host.Children[0]);
|
||||
Assert.Equal("Masa [t]: 440", figure.Inlines!.Text);
|
||||
Assert.Equal(22, figure.Margin.Right);
|
||||
Assert.Equal(0, ((TextBlock)host.Children[1]).Margin.Right);
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,20 @@ public sealed class PhysicalFileSystemTests : IDisposable
|
||||
Assert.Equal(new[] { path }, Directory.GetFiles(_root));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_pattern_lists_names_in_any_letter_case()
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(_root, "PKP", "EP09"));
|
||||
foreach (string name in new[] { "Braniewo.SCN", "kato.scn", "notes.txt", Path.Combine("PKP", "EP09", "EP09.FIZ"),
|
||||
Path.Combine("PKP", "EP09", "ep09dumb.fiz") })
|
||||
File.WriteAllText(Path.Combine(_root, name), "");
|
||||
|
||||
Assert.Equal(new[] { "Braniewo.SCN", "kato.scn" },
|
||||
_files.GetFiles(_root, "*.scn").Select(Path.GetFileName).Order(StringComparer.Ordinal));
|
||||
Assert.Equal(new[] { "EP09.FIZ", "ep09dumb.fiz" },
|
||||
_files.GetFilesRecursive(_root, "*.fiz").Select(Path.GetFileName).Order(StringComparer.Ordinal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_atomic_write_that_cannot_finish_leaves_what_stood_there()
|
||||
{
|
||||
|
||||
@@ -65,6 +65,19 @@ public class PhysicsAndAssetsTests
|
||||
Assert.Equal(125, physics.VMax);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_include_named_in_other_case_is_followed()
|
||||
{
|
||||
var installation = new TestInstallation(new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile(Fiz("pkp", "ep07", "ep07"), "include Common.FIZ 84000 end")
|
||||
.WithFile(TestInstallation.At("dynamic", "pkp", "ep07", "common.fiz"), "Param: M=(1) Vmax=125"));
|
||||
|
||||
var physics = installation.Physics.For("pkp/ep07/", "ep07")!;
|
||||
|
||||
Assert.Equal(84000, physics.Mass);
|
||||
Assert.Equal(125, physics.VMax);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_dot_in_the_model_name_is_part_of_the_name()
|
||||
{
|
||||
@@ -202,4 +215,31 @@ public class PhysicsAndAssetsTests
|
||||
|
||||
Assert.Empty(installation.MissingAssets.Scan(installation.Library.Vehicles));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_livery_with_no_folder_is_looked_for_in_dynamic_itself()
|
||||
{
|
||||
var installation = new TestInstallation(new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile(TestInstallation.At("dynamic", "EP09.bmp"), "bitmap")
|
||||
.WithFile(TestInstallation.At("dynamic", "ep09.e3d"), "model"));
|
||||
var catalog = new StarterNG.Domain.Vehicles.VehicleCatalog(installation.MiniTextures);
|
||||
catalog.Textures.Add(new StarterNG.Domain.Vehicles.VehicleTexture { Directory = "", Skinfile = "ep09", Model = "EP09" });
|
||||
|
||||
Assert.Empty(installation.MissingAssets.Scan(catalog));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_skin_and_model_on_disk_in_other_case_are_not_reported()
|
||||
{
|
||||
var files = new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile(TestInstallation.At("dynamic", "PKP", "EP09", "textures.txt"),
|
||||
"EP09-014.mat=ep09.e3d,ep09")
|
||||
.WithFile(TestInstallation.At("dynamic", "PKP", "EP09", "ep09-014.BMP"), "bitmap")
|
||||
.WithFile(TestInstallation.At("dynamic", "PKP", "EP09", "EP09.E3D"), "model");
|
||||
var installation = new TestInstallation(files);
|
||||
installation.Vehicles.Load(installation.Library.Vehicles);
|
||||
Assert.NotEmpty(installation.Library.Vehicles.Textures);
|
||||
|
||||
Assert.Empty(installation.MissingAssets.Scan(installation.Library.Vehicles));
|
||||
}
|
||||
}
|
||||
|
||||
76
StarterNG.Tests/ReadableSliderTests.cs
Normal file
76
StarterNG.Tests/ReadableSliderTests.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System.Globalization;
|
||||
using Avalonia.Automation;
|
||||
using Avalonia.Automation.Peers;
|
||||
using Avalonia.Automation.Provider;
|
||||
using Avalonia.Controls;
|
||||
using StarterNG.Controls;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class ReadableSliderTests
|
||||
{
|
||||
[Fact]
|
||||
public void The_track_under_the_mouse_says_the_name_and_the_value()
|
||||
{
|
||||
var fog = new ReadableSlider { ValueText = "2,4 km" };
|
||||
Assert.Equal("2,4 km", fog.TrackText);
|
||||
|
||||
AutomationProperties.SetName(fog, "Widoczność ");
|
||||
Assert.Equal("Widoczność: 2,4 km", fog.TrackText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_slider_is_read_as_the_value_shown_beside_it()
|
||||
{
|
||||
var fog = new ReadableSlider { Minimum = 0, Maximum = 1000, Value = 523, ValueText = " 2,4 km " };
|
||||
|
||||
Assert.Equal("2,4 km", fog.SpokenValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Without_a_shown_value_the_number_itself_is_read()
|
||||
{
|
||||
var before = CultureInfo.CurrentCulture;
|
||||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("pl-PL");
|
||||
try
|
||||
{
|
||||
Assert.Equal("-5", ReadableSlider.SpokenText(null, -5));
|
||||
Assert.Equal("1,25", ReadableSlider.SpokenText(" ", 1.25));
|
||||
}
|
||||
finally
|
||||
{
|
||||
CultureInfo.CurrentCulture = before;
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_setting_row_gives_its_value_to_the_slider_in_it()
|
||||
{
|
||||
var slider = new ReadableSlider { Minimum = 0, Maximum = 100, Value = 40 };
|
||||
var row = new SettingRow { Content = new Border { Child = slider } };
|
||||
|
||||
row.ValueText = "40%";
|
||||
Assert.Equal("40%", slider.SpokenValue);
|
||||
|
||||
row.ValueText = null;
|
||||
Assert.Equal("40", slider.SpokenValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_screen_reader_is_given_the_text_and_may_set_a_number()
|
||||
{
|
||||
var slider = new ReadableSlider { Minimum = 0, Maximum = 1000, Value = 523, ValueText = "2 km" };
|
||||
var peer = ControlAutomationPeer.CreatePeerForElement(slider);
|
||||
var value = Assert.IsAssignableFrom<IValueProvider>(peer);
|
||||
|
||||
Assert.Equal(AutomationControlType.Slider, peer.GetAutomationControlType());
|
||||
Assert.Equal("Slider", peer.GetClassName());
|
||||
Assert.Equal("2 km", value.Value);
|
||||
Assert.False(value.IsReadOnly);
|
||||
|
||||
value.SetValue("300");
|
||||
Assert.Equal(300, slider.Value);
|
||||
Assert.Throws<ArgumentException>(() => value.SetValue("dużo"));
|
||||
}
|
||||
}
|
||||
@@ -182,6 +182,20 @@ public class TimetableLocatorTests
|
||||
Assert.NotNull(new TimetableLocator(files).Resolve(scenery, "r1.txt"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(@"rozklady\tgs3657")]
|
||||
[InlineData("ROZKLADY/TGS3657.TXT")]
|
||||
public void A_timetable_named_in_other_case_or_with_backslashes_is_found_as_on_disk(string name)
|
||||
{
|
||||
var files = new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile(TestInstallation.At("scenery", "Braniewo.scn"), "//$n Braniewo")
|
||||
.WithFile(TestInstallation.At("scenery", "Rozklady", "TGS3657.txt"), "rozkład");
|
||||
var scenery = Assert.Single(new TestInstallation(files).Sceneries.LoadAll());
|
||||
|
||||
Assert.Equal(TestInstallation.At("scenery", "Rozklady", "TGS3657.txt"),
|
||||
new TimetableLocator(files).Resolve(scenery, name));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_formats_none_and_a_missing_file_both_resolve_to_nothing()
|
||||
{
|
||||
@@ -195,3 +209,26 @@ public class TimetableLocatorTests
|
||||
Assert.Null(locator.Resolve(scenery, null));
|
||||
}
|
||||
}
|
||||
|
||||
public class AttachmentLocatorTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(@"scenery\Braniewo\opis.PDF")]
|
||||
[InlineData("SCENERY/braniewo/Opis.pdf")]
|
||||
public void An_attachment_named_in_other_case_is_found_as_on_disk(string named)
|
||||
{
|
||||
var installation = new TestInstallation(new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile(TestInstallation.At("scenery", "Braniewo", "Opis.pdf"), "%PDF"));
|
||||
|
||||
Assert.Equal(TestInstallation.At("scenery", "Braniewo", "Opis.pdf"),
|
||||
new AttachmentLocator(installation.Files, installation.Paths)
|
||||
.Resolve(new SceneryAttachment { FilePath = named }));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("scenery/brak.pdf")]
|
||||
[InlineData("")]
|
||||
public void An_attachment_that_is_not_there_is_not_found(string named) =>
|
||||
Assert.Null(new AttachmentLocator(new InMemoryFileSystem(caseSensitive: true), new TestInstallation().Paths)
|
||||
.Resolve(new SceneryAttachment { FilePath = named }));
|
||||
}
|
||||
|
||||
@@ -89,6 +89,23 @@ public class SceneryRepositoryTests
|
||||
Assert.Equal(Path.Combine("game", "scenery", "images", "kato.jpg"), image);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("calkowo_v2_kopalniaA.jpg", "game/scenery/images/calkowo_v2_kopalniaA.jpg")]
|
||||
[InlineData(@"Images\Calkowo_V2_KopalniaA.JPG", "game/scenery/images/calkowo_v2_kopalniaA.jpg")]
|
||||
[InlineData("CALKOWO_V2.JPG", "game/scenery/Calkowo_v2.jpg")]
|
||||
public void The_preview_image_is_found_whatever_the_case_it_is_named_in(string named, string onDisk)
|
||||
{
|
||||
var files = new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile("game/scenery/calkowo.scn", "//$i " + named)
|
||||
.WithFile("game/scenery/images/calkowo_v2_kopalniaA.jpg", "binary")
|
||||
.WithFile("game/scenery/Calkowo_v2.jpg", "binary");
|
||||
var scenery = Assert.Single(Build(files, out _).LoadAll());
|
||||
|
||||
string? image = new SceneryImageLocator(files).Resolve(scenery);
|
||||
|
||||
Assert.Equal(Path.GetFullPath(onDisk), Path.GetFullPath(image!));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_scenery_without_an_image_directive_resolves_to_nothing()
|
||||
{
|
||||
|
||||
@@ -273,7 +273,7 @@ public class ScenerySyntaxTests
|
||||
include koniec.scm
|
||||
""";
|
||||
|
||||
var scenery = TestScenery.Parser().Parse("scenery/x.scn", scn, _ => new IncludeLookup(true));
|
||||
var scenery = TestScenery.Parser().Parse("scenery/x.scn", scn, _ => true);
|
||||
|
||||
Assert.Empty(scenery.Faults);
|
||||
Assert.Equal("td/rozklad", Assert.Single(scenery.Trainsets).Name);
|
||||
@@ -326,7 +326,7 @@ public class ScenerySyntaxTests
|
||||
endoptional
|
||||
""";
|
||||
|
||||
var faults = TestScenery.Parser().Parse("scenery/x.scn", scn, name => new IncludeLookup(name is "tory.scm" or "budynek.inc")).Faults;
|
||||
var faults = TestScenery.Parser().Parse("scenery/x.scn", scn, name => name is "tory.scm" or "budynek.inc").Faults;
|
||||
|
||||
Assert.Equal((SceneryFaultKind.MissingInclude, "x.scn", 2, null, "brak.scm"), Seen(Assert.Single(faults)));
|
||||
}
|
||||
@@ -347,9 +347,9 @@ public class ScenerySyntaxTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_include_on_disk_only_in_other_letter_case_is_what_linux_misses()
|
||||
public void An_include_on_disk_in_other_letter_case_is_no_fault()
|
||||
{
|
||||
// The simulator folds the name to lower case and opens it so.
|
||||
// The simulator finds the file whatever the case of its letters, on Linux too.
|
||||
var files = new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithLegacyFile("game/scenery/braniewo_szeroki.scn",
|
||||
"include braniewo/base.ctr end\ninclude Braniewo/Tory.SCM end\n")
|
||||
@@ -357,21 +357,20 @@ public class ScenerySyntaxTests
|
||||
.WithFile("game/scenery/braniewo/tory.scm", "");
|
||||
var repository = TestScenery.Repository(files);
|
||||
|
||||
var faults = repository.Load("game/scenery/braniewo_szeroki.scn")!.Faults;
|
||||
|
||||
Assert.Equal((SceneryFaultKind.IncludeLetterCase, "braniewo_szeroki.scn", 1, "Braniewo/Base.ctr", "braniewo/base.ctr"),
|
||||
Seen(Assert.Single(faults)));
|
||||
Assert.Empty(repository.Load("game/scenery/braniewo_szeroki.scn")!.Faults);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(true)]
|
||||
[InlineData(false)]
|
||||
public void A_missing_terrain_file_is_no_fault_while_the_scenery_has_its_own_sbt(bool sbt)
|
||||
[InlineData(null)]
|
||||
[InlineData("lk2_sandbox.sbt")]
|
||||
[InlineData("Lk2_Sandbox.SBT")]
|
||||
public void A_missing_terrain_file_is_no_fault_while_the_scenery_has_its_own_sbt(string? sbtOnDisk)
|
||||
{
|
||||
bool sbt = sbtOnDisk is not null;
|
||||
var files = new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithLegacyFile("game/scenery/Lk2_Sandbox.scn", "include nmt/nmt100_mazowieckie_ter.scm end\nFirstInit\n");
|
||||
if (sbt)
|
||||
files.WithFile("game/scenery/lk2_sandbox.sbt", "EU07");
|
||||
files.WithFile("game/scenery/" + sbtOnDisk, "EU07");
|
||||
var repository = TestScenery.Repository(files);
|
||||
|
||||
var scenery = repository.Load("game/scenery/Lk2_Sandbox.scn")!;
|
||||
|
||||
@@ -39,6 +39,24 @@ public class SceneryTranslationsTests
|
||||
Assert.Equal("Polski", scenery.LocalizedDescription(translations));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_tables_are_found_whatever_the_case_of_their_names()
|
||||
{
|
||||
var files = new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile(TestInstallation.At("scenery", "Braniewo.scn"), "//$d @brief")
|
||||
.WithFile(Path.Combine(Path.GetFullPath(TestInstallation.At("scenery")), "I18N", "braniewo_en.json"),
|
||||
"""{ "brief": "English" }""")
|
||||
.WithFile(Path.Combine(Path.GetFullPath(TestInstallation.At("scenery")), "I18N", "BRANIEWO_PL.json"),
|
||||
"""{ "brief": "Polski" }""");
|
||||
var installation = new TestInstallation(files);
|
||||
var scenery = Assert.Single(installation.Sceneries.LoadAll());
|
||||
var translations = new SceneryTranslations(files, installation.Log);
|
||||
|
||||
translations.LoadFor(scenery, "Polski");
|
||||
|
||||
Assert.Equal("Polski", scenery.LocalizedDescription(translations));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Literal_text_and_unknown_keys_are_left_alone()
|
||||
{
|
||||
|
||||
229
StarterNG.Tests/ScreenReaderNamesTests.cs
Normal file
229
StarterNG.Tests/ScreenReaderNamesTests.cs
Normal file
@@ -0,0 +1,229 @@
|
||||
using System.Globalization;
|
||||
using Avalonia.Automation;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Documents;
|
||||
using StarterNG.Controls;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class ScreenReaderNamesTests
|
||||
{
|
||||
static ScreenReaderNamesTests() => ScreenReaderNames.Install();
|
||||
|
||||
private static StackPanel Row(params Control[] children)
|
||||
{
|
||||
var panel = new StackPanel();
|
||||
panel.Children.AddRange(children);
|
||||
return panel;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_icon_button_is_named_by_its_tooltip()
|
||||
{
|
||||
var button = new Button { Content = new Image() };
|
||||
ToolTip.SetTip(button, "Otwórz ostatni log symulatora");
|
||||
|
||||
Assert.Equal("Otwórz ostatni log symulatora", AutomationProperties.GetName(button));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_button_without_a_tooltip_is_named_by_the_text_it_shows()
|
||||
{
|
||||
var button = new Button { Content = Row(new Image(), new TextBlock { Text = "Dodaj" }) };
|
||||
|
||||
Assert.Equal("Dodaj", AutomationProperties.GetName(button));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_button_with_text_of_its_own_keeps_it()
|
||||
{
|
||||
var button = new Button { Content = "Zapisz" };
|
||||
ToolTip.SetTip(button, "Zapisz ustawienia");
|
||||
|
||||
Assert.Null(AutomationProperties.GetName(button));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_name_given_on_purpose_is_left_alone()
|
||||
{
|
||||
var button = new Button();
|
||||
AutomationProperties.SetName(button, "Discord");
|
||||
button.Content = new Image();
|
||||
ToolTip.SetTip(button, "Dołącz do nas");
|
||||
|
||||
Assert.Equal("Discord", AutomationProperties.GetName(button));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_new_tooltip_replaces_the_name_it_gave()
|
||||
{
|
||||
var button = new Button { Content = new Image() };
|
||||
ToolTip.SetTip(button, "Undo");
|
||||
ToolTip.SetTip(button, "Cofnij");
|
||||
|
||||
Assert.Equal("Cofnij", AutomationProperties.GetName(button));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Text_put_on_a_button_later_takes_back_the_name_it_was_given()
|
||||
{
|
||||
var button = new Button { Content = new Image() };
|
||||
ToolTip.SetTip(button, "Pomoc");
|
||||
button.Content = "Pomoc";
|
||||
|
||||
Assert.Null(AutomationProperties.GetName(button));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_button_with_only_a_sign_on_it_is_named_by_its_tooltip()
|
||||
{
|
||||
var button = new Button { Content = "?" };
|
||||
ToolTip.SetTip(button, "Pomoc");
|
||||
|
||||
Assert.Equal("Pomoc", AutomationProperties.GetName(button));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_tree_entry_is_named_by_the_visible_text_of_its_header()
|
||||
{
|
||||
var hidden = new TextBlock { Text = "ukryte", IsVisible = false };
|
||||
var item = new TreeViewItem
|
||||
{
|
||||
Header = Row(new Image(), new TextBlock { Text = "baltyk_cargo" }, hidden, new TextBlock { Text = " " })
|
||||
};
|
||||
|
||||
Assert.Equal("baltyk_cargo", AutomationProperties.GetName(item));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_list_entry_joins_the_texts_it_shows()
|
||||
{
|
||||
var item = new ListBoxItem
|
||||
{
|
||||
Content = new Border { Child = Row(new TextBlock { Text = "PKPIC" }, new TextBlock { Text = "303E-EP-426" }) }
|
||||
};
|
||||
|
||||
Assert.Equal("PKPIC, 303E-EP-426", AutomationProperties.GetName(item));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Text_made_of_runs_is_read_whole()
|
||||
{
|
||||
var block = new TextBlock();
|
||||
block.Inlines!.Add(new Run("EP07"));
|
||||
block.Inlines.Add(new LineBreak());
|
||||
block.Inlines.Add(new Span { Inlines = { new Run("544") } });
|
||||
|
||||
Assert.Equal("EP07 544", ScreenReaderNames.TextOf(block));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Nothing_to_read_gives_no_name()
|
||||
{
|
||||
Assert.Null(ScreenReaderNames.TextOf(null));
|
||||
Assert.Null(ScreenReaderNames.TextOf(new Image()));
|
||||
Assert.Null(ScreenReaderNames.TextOf(" "));
|
||||
Assert.Equal("Opis", ScreenReaderNames.TextOf(new ContentControl { Content = " Opis " }));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_field_is_named_by_the_hint_written_in_it()
|
||||
{
|
||||
var search = new TextBox { PlaceholderText = "Szukaj…" };
|
||||
var category = new ComboBox { PlaceholderText = "Kategoria" };
|
||||
|
||||
Assert.Equal("Szukaj…", AutomationProperties.GetName(search));
|
||||
Assert.Equal("Kategoria", AutomationProperties.GetName(category));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_setting_row_names_its_inputs_by_its_label()
|
||||
{
|
||||
var combo = new ComboBox();
|
||||
var slider = new Slider();
|
||||
var browse = new Button { Content = "Wybierz plik…" };
|
||||
var row = new SettingRow { Content = new Border { Child = Row(combo, slider, browse) } };
|
||||
row.Label = "Wybór exe";
|
||||
|
||||
Assert.Equal("Wybór exe", AutomationProperties.GetName(combo));
|
||||
Assert.Equal("Wybór exe", AutomationProperties.GetName(slider));
|
||||
Assert.Null(AutomationProperties.GetName(browse));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_setting_row_describes_its_inputs_with_its_explanation()
|
||||
{
|
||||
var check = new CheckBox { Content = "Tryb pełnoekranowy" };
|
||||
var slider = new Slider();
|
||||
var row = new SettingRow { Label = "Ekran", Content = Row(check, slider) };
|
||||
row.Description = "Gra zajmie cały ekran.";
|
||||
row.IdleNote = "Ten symulator nie zna tego ustawienia.";
|
||||
|
||||
Assert.Equal("Gra zajmie cały ekran., Ten symulator nie zna tego ustawienia.", AutomationProperties.GetHelpText(check));
|
||||
Assert.Equal(AutomationProperties.GetHelpText(check), AutomationProperties.GetHelpText(slider));
|
||||
Assert.Null(AutomationProperties.GetName(check));
|
||||
|
||||
row.Description = null;
|
||||
row.IdleNote = null;
|
||||
Assert.Null(AutomationProperties.GetHelpText(check));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_description_given_on_purpose_is_left_alone()
|
||||
{
|
||||
var box = new TextBox();
|
||||
AutomationProperties.SetHelpText(box, "Ścieżka do exe");
|
||||
_ = new SettingRow { Content = box, Description = "Plik symulatora" };
|
||||
|
||||
Assert.Equal("Ścieżka do exe", AutomationProperties.GetHelpText(box));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_setting_row_without_a_label_leaves_its_input_unnamed()
|
||||
{
|
||||
var box = new TextBox();
|
||||
_ = new SettingRow { Content = box };
|
||||
|
||||
Assert.Null(AutomationProperties.GetName(box));
|
||||
}
|
||||
}
|
||||
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class PickerNameTests
|
||||
{
|
||||
[Fact]
|
||||
public void A_picker_is_read_as_its_label_and_its_value()
|
||||
{
|
||||
Assert.Equal("Godzina: 07:05", ScreenReaderNames.PickerText("Godzina", "07:05"));
|
||||
Assert.Equal("Data", ScreenReaderNames.PickerText("Data", null));
|
||||
Assert.Equal("07:05", ScreenReaderNames.PickerText(null, "07:05"));
|
||||
Assert.Null(ScreenReaderNames.PickerText("", null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_date_is_read_in_words_with_the_year_only_when_shown()
|
||||
{
|
||||
var before = CultureInfo.CurrentCulture;
|
||||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("pl-PL");
|
||||
try
|
||||
{
|
||||
var date = new DateTimeOffset(2026, 10, 27, 0, 0, 0, TimeSpan.FromHours(1));
|
||||
|
||||
Assert.Equal("27 października", ScreenReaderNames.DateText(date, withYear: false));
|
||||
Assert.Equal("wtorek, 27 października 2026", ScreenReaderNames.DateText(date, withYear: true));
|
||||
Assert.Null(ScreenReaderNames.DateText(null, withYear: false));
|
||||
}
|
||||
finally
|
||||
{
|
||||
CultureInfo.CurrentCulture = before;
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_time_is_read_as_hours_and_minutes()
|
||||
{
|
||||
Assert.Equal("07:05", ScreenReaderNames.TimeText(new TimeSpan(7, 5, 30)));
|
||||
Assert.Null(ScreenReaderNames.TimeText(null));
|
||||
}
|
||||
}
|
||||
45
StarterNG.Tests/SoftwareRenderTests.cs
Normal file
45
StarterNG.Tests/SoftwareRenderTests.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using Avalonia;
|
||||
using StarterNG.Infrastructure;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
public class SoftwareRenderTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("--software-render")]
|
||||
[InlineData("--Software-Render")]
|
||||
public void The_switch_asks_for_it_in_any_case(string arg)
|
||||
{
|
||||
Assert.True(SoftwareRender.AskedIn(new[] { "--other", arg }));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Without_the_switch_the_graphics_card_draws()
|
||||
{
|
||||
Assert.False(SoftwareRender.AskedIn(Array.Empty<string>()));
|
||||
Assert.False(SoftwareRender.AskedIn(new[] { "--software" }));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Windows_draws_on_the_processor_into_a_plain_window_surface()
|
||||
{
|
||||
var options = SoftwareRender.Win32Options;
|
||||
|
||||
Assert.Equal(new[] { Win32RenderingMode.Software }, options.RenderingMode);
|
||||
Assert.Equal(new[] { Win32CompositionMode.RedirectionSurface }, options.CompositionMode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void X11_draws_on_the_processor()
|
||||
{
|
||||
Assert.Equal(new[] { X11RenderingMode.Software }, SoftwareRender.X11Options.RenderingMode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Both_platforms_options_go_to_the_same_builder()
|
||||
{
|
||||
var builder = AppBuilder.Configure<Avalonia.Application>();
|
||||
|
||||
Assert.Same(builder, SoftwareRender.ApplyTo(builder));
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia.Headless" Version="12.1.4" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
|
||||
98
StarterNG.Tests/SystemGraphicsLibrariesTests.cs
Normal file
98
StarterNG.Tests/SystemGraphicsLibrariesTests.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using StarterNG.Infrastructure;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
public class SystemGraphicsLibrariesTests
|
||||
{
|
||||
private const string System32 = @"C:\Windows\System32";
|
||||
private const string Game = @"E:\SteamLibrary\steamapps\common\MaSzyna";
|
||||
|
||||
private static readonly IntPtr Dxgi = new(0x1000);
|
||||
private static readonly IntPtr D3d11 = new(0x2000);
|
||||
|
||||
private static SystemGraphicsLibraries Take(Func<string, bool> beside, Dictionary<string, IntPtr> system)
|
||||
{
|
||||
return SystemGraphicsLibraries.Take(System32, Game, beside, Load);
|
||||
|
||||
bool Load(string path, out IntPtr handle)
|
||||
{
|
||||
handle = system.GetValueOrDefault(Path.GetFileName(path));
|
||||
return handle != IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
private static SystemGraphicsLibraries WithDxgiAndD3d11(Func<string, bool> beside) =>
|
||||
Take(beside, new Dictionary<string, IntPtr> { ["dxgi.dll"] = Dxgi, ["d3d11.dll"] = D3d11 });
|
||||
|
||||
[Fact]
|
||||
public void An_import_by_name_gets_the_system_library_with_or_without_its_extension()
|
||||
{
|
||||
var taken = WithDxgiAndD3d11(_ => false);
|
||||
|
||||
Assert.Equal(Dxgi, taken.Resolve("dxgi"));
|
||||
Assert.Equal(Dxgi, taken.Resolve("dxgi.dll"));
|
||||
Assert.Equal(D3d11, taken.Resolve("D3D11.DLL"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Other_libraries_and_ones_the_system_lacks_are_left_to_the_usual_search()
|
||||
{
|
||||
var taken = WithDxgiAndD3d11(_ => false);
|
||||
|
||||
Assert.Equal(IntPtr.Zero, taken.Resolve("user32.dll"));
|
||||
Assert.Equal(IntPtr.Zero, taken.Resolve("opengl32"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void They_are_loaded_from_the_system_folder_dxgi_first()
|
||||
{
|
||||
var asked = new List<string>();
|
||||
SystemGraphicsLibraries.Take(System32, Game, _ => false, Load);
|
||||
|
||||
Assert.Equal(SystemGraphicsLibraries.Names.Select(n => Path.Combine(System32, n + ".dll")), asked);
|
||||
Assert.Equal("dxgi", SystemGraphicsLibraries.Names[0]);
|
||||
|
||||
bool Load(string path, out IntPtr handle)
|
||||
{
|
||||
asked.Add(path);
|
||||
handle = IntPtr.Zero;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_wrapper_beside_the_starter_is_noted()
|
||||
{
|
||||
var taken = WithDxgiAndD3d11(path => path == Path.Combine(Game, "dxgi.dll"));
|
||||
|
||||
Assert.Equal(new[] { "dxgi.dll" }, taken.Shadowed);
|
||||
Assert.Contains("dxgi.dll", taken.Note);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void With_nothing_beside_the_starter_there_is_nothing_to_note()
|
||||
{
|
||||
var taken = WithDxgiAndD3d11(_ => false);
|
||||
|
||||
Assert.Empty(taken.Shadowed);
|
||||
Assert.Null(taken.Note);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Only_windows_has_system_libraries_to_take()
|
||||
{
|
||||
var taken = SystemGraphicsLibraries.TakeOnWindows();
|
||||
|
||||
Assert.Equal(OperatingSystem.IsWindows(), taken is not null);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_assembly_with_a_resolver_of_its_own_keeps_it()
|
||||
{
|
||||
var taken = WithDxgiAndD3d11(_ => false);
|
||||
var assembly = typeof(SystemGraphicsLibrariesTests).Assembly;
|
||||
|
||||
Assert.True(taken.ServeTo(assembly));
|
||||
Assert.False(taken.ServeTo(assembly));
|
||||
}
|
||||
}
|
||||
@@ -130,6 +130,17 @@ public class TapeLaunchTests
|
||||
Assert.Null(new StarterNG.Infrastructure.Sceneries.TimetableLocator(new InMemoryFileSystem())
|
||||
.TrainNumber(new StarterNG.Domain.Sceneries.Scenery(TestInstallation.At("scenery", "td.scn"), ""), name));
|
||||
|
||||
[Fact]
|
||||
public void The_train_number_comes_from_a_timetable_named_in_other_case_with_backslashes()
|
||||
{
|
||||
var files = new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile(TestInstallation.At("scenery", "Baltyk", "EIE1531.txt"), "[ Rodzaj i numer pociagu | EIE1531 ]\r\n");
|
||||
|
||||
Assert.Equal("EIE1531", new StarterNG.Infrastructure.Sceneries.TimetableLocator(files)
|
||||
.TrainNumber(new StarterNG.Domain.Sceneries.Scenery(TestInstallation.At("scenery", "td.scn"), ""),
|
||||
@"baltyk\eie1531"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_timetable_that_is_not_there_gives_no_train_number() =>
|
||||
Assert.Null(new StarterNG.Infrastructure.Sceneries.TimetableLocator(new InMemoryFileSystem())
|
||||
|
||||
175
StarterNG.Tests/TapeViewTests.cs
Normal file
175
StarterNG.Tests/TapeViewTests.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using Avalonia.Automation.Peers;
|
||||
using Avalonia.Automation.Provider;
|
||||
using Avalonia.Input;
|
||||
using StarterNG.Application;
|
||||
using StarterNG.Domain.Tapes;
|
||||
using StarterNG.Tests.Fakes;
|
||||
using StarterNG.Views.Tapes;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
/// <summary>A recorded trip walked from the keyboard and read by a screen reader, on both ways of drawing it.</summary>
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class TapeViewTests
|
||||
{
|
||||
// The electronic tape writes its legend in the starter's language.
|
||||
static TapeViewTests()
|
||||
{
|
||||
if (AppServices.CurrentOrNull is null)
|
||||
{
|
||||
var installation = new TestInstallation(new InMemoryFileSystem());
|
||||
AppServices.Initialize(installation.Paths, installation.Files, installation.Clock, installation.Environment);
|
||||
}
|
||||
}
|
||||
|
||||
public static TheoryData<string> Views => new() { "hasler", "electronic" };
|
||||
|
||||
private static TapeView View(string kind) => kind == "hasler" ? new HaslerTapeView() : new ElectronicTapeView();
|
||||
|
||||
/// <summary>Two minutes, a sample a second, 15 m a second; running from 10 s to 110 s, braking after 100 s.</summary>
|
||||
private static HaslerTape Trip() =>
|
||||
new(Enumerable.Range(0, 121)
|
||||
.Select(i => new TapeSample(i, i is > 10 and < 110 ? 54 : 0, i * 15.0, i > 100 ? 2 : 0, 5,
|
||||
i < 50 ? 300 : 0, i == 70))
|
||||
.ToList());
|
||||
|
||||
private static (TapeView View, List<TapeSample?> Hovered) Walking(string kind, bool byDistance = true)
|
||||
{
|
||||
var view = View(kind);
|
||||
view.Tape = Trip();
|
||||
view.ByDistance = byDistance;
|
||||
var hovered = new List<TapeSample?>();
|
||||
view.Hovered += s => hovered.Add(s);
|
||||
return (view, hovered);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Views))]
|
||||
public void The_trip_is_a_slider_read_as_the_moment_under_the_cursor(string kind)
|
||||
{
|
||||
var view = View(kind);
|
||||
var peer = Controls.Peer(view);
|
||||
var value = Assert.IsAssignableFrom<IValueProvider>(peer);
|
||||
|
||||
view.Spoken = "10:00:12, km 0,2, 54 km/h";
|
||||
|
||||
Assert.Equal(AutomationControlType.Slider, peer.GetAutomationControlType());
|
||||
Assert.True(peer.IsContentElement());
|
||||
Assert.True(peer.IsControlElement());
|
||||
Assert.Equal("10:00:12, km 0,2, 54 km/h", value.Value);
|
||||
Assert.True(value.IsReadOnly);
|
||||
Assert.Throws<InvalidOperationException>(() => value.SetValue("x"));
|
||||
|
||||
view.Spoken = "10:00:13, km 0,2, 54 km/h";
|
||||
Assert.Equal("10:00:13, km 0,2, 54 km/h", value.Value);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Views))]
|
||||
public void The_first_arrow_starts_at_the_beginning_and_the_next_go_a_hundred_metres(string kind)
|
||||
{
|
||||
var (view, hovered) = Walking(kind);
|
||||
|
||||
Assert.True(Controls.Press(view, Key.Right));
|
||||
Assert.Equal(0, hovered[^1]!.Value.Time);
|
||||
|
||||
Assert.True(Controls.Press(view, Key.Right));
|
||||
Assert.InRange(hovered[^1]!.Value.DistanceM, TapeView.DistanceStep - 15, TapeView.DistanceStep + 15);
|
||||
|
||||
Assert.True(Controls.Press(view, Key.Right, KeyModifiers.Control));
|
||||
Assert.True(hovered[^1]!.Value.DistanceM >= TapeView.DistanceStep * TapeView.CtrlTimes - 15);
|
||||
|
||||
Assert.True(Controls.Press(view, Key.Left, KeyModifiers.Control));
|
||||
Assert.True(hovered[^1]!.Value.DistanceM <= TapeView.DistanceStep + 15);
|
||||
|
||||
Assert.True(Controls.Press(view, Key.End));
|
||||
Assert.Equal(120, hovered[^1]!.Value.Time);
|
||||
Assert.True(Controls.Press(view, Key.Right));
|
||||
Assert.Equal(120, hovered[^1]!.Value.Time);
|
||||
|
||||
Assert.True(Controls.Press(view, Key.Home));
|
||||
Assert.Equal(0, hovered[^1]!.Value.Time);
|
||||
Assert.True(Controls.Press(view, Key.Left));
|
||||
Assert.Equal(0, hovered[^1]!.Value.Time);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Views))]
|
||||
public void By_time_an_arrow_goes_ten_seconds(string kind)
|
||||
{
|
||||
var (view, hovered) = Walking(kind, byDistance: false);
|
||||
|
||||
Controls.Press(view, Key.Left);
|
||||
Assert.Equal(0, hovered[^1]!.Value.Time);
|
||||
Controls.Press(view, Key.Right);
|
||||
Assert.Equal(TapeView.TimeStep, hovered[^1]!.Value.Time);
|
||||
Controls.Press(view, Key.Left);
|
||||
Assert.Equal(0, hovered[^1]!.Value.Time);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Views))]
|
||||
public void Enter_picks_the_sample_the_keys_stand_at_once_they_have_moved(string kind)
|
||||
{
|
||||
var (view, _) = Walking(kind);
|
||||
TapeSample? picked = null;
|
||||
view.Picked += s => picked = s;
|
||||
|
||||
Assert.False(Controls.Press(view, Key.Enter));
|
||||
Assert.Null(picked);
|
||||
|
||||
Controls.Press(view, Key.End);
|
||||
Assert.True(Controls.Press(view, Key.Enter));
|
||||
Assert.Equal(120, picked!.Value.Time);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Views))]
|
||||
public void A_new_trip_starts_the_keys_from_its_beginning(string kind)
|
||||
{
|
||||
var (view, hovered) = Walking(kind);
|
||||
Controls.Press(view, Key.End);
|
||||
|
||||
view.Tape = Trip();
|
||||
Controls.Press(view, Key.Right);
|
||||
|
||||
Assert.Equal(0, hovered[^1]!.Value.Time);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Views))]
|
||||
public void Other_keys_and_an_empty_trip_are_left_alone(string kind)
|
||||
{
|
||||
var (view, hovered) = Walking(kind);
|
||||
var empty = View(kind);
|
||||
var blank = View(kind);
|
||||
blank.Tape = new HaslerTape([]);
|
||||
|
||||
Assert.False(Controls.Press(view, Key.A));
|
||||
Assert.False(Controls.Press(empty, Key.Right));
|
||||
Assert.False(Controls.Press(blank, Key.Right));
|
||||
Assert.Empty(hovered);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Views))]
|
||||
public void The_trip_draws_with_and_without_the_focus(string kind)
|
||||
{
|
||||
var (view, _) = Walking(kind);
|
||||
view.Mark = 60;
|
||||
var window = Controls.Show(view, 1200, 600);
|
||||
try
|
||||
{
|
||||
Assert.True(view.Focusable);
|
||||
view.Focus();
|
||||
Controls.Press(view, Key.Right);
|
||||
Controls.Settle();
|
||||
window.Focus();
|
||||
Controls.Settle();
|
||||
}
|
||||
finally
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
22
StarterNG.Tests/TextureFolderTests.cs
Normal file
22
StarterNG.Tests/TextureFolderTests.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using StarterNG.Domain.Vehicles;
|
||||
using StarterNG.Infrastructure.Vehicles;
|
||||
using StarterNG.Tests.Fakes;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
public class TextureFolderTests
|
||||
{
|
||||
private static readonly TestInstallation Installation = new(new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile("game/dynamic/PKP/EP09_v1/textures.txt", "")
|
||||
.WithDirectory("game/Other/Folder"));
|
||||
|
||||
[Theory]
|
||||
[InlineData("pkp/ep09_v1/", "dynamic/PKP/EP09_v1")]
|
||||
[InlineData(@"PKP\EP09_V1", "dynamic/PKP/EP09_v1")]
|
||||
[InlineData("other/folder/", "Other/Folder")]
|
||||
[InlineData("pkp/brak/", "pkp/brak")]
|
||||
public void A_liverys_folder_is_found_as_it_is_named_on_disk(string named, string onDisk) =>
|
||||
Assert.Equal(Path.GetFullPath(Path.Combine("game", onDisk)),
|
||||
Path.GetFullPath(TextureFolder.Of(Installation.Files, Installation.Paths,
|
||||
new VehicleTexture { Directory = named })));
|
||||
}
|
||||
Reference in New Issue
Block a user