Files
StarterNG/StarterNG.Tests/SettingsUnknownValueTests.cs
Hirek 7c2d259fdc feat(settings): the physical models of the simulator, and Arcade and Realistic levels of realism
The Physics tab has a section of driving realism with the models the simulator
reads (utilities/Globals.cpp):
- couplerphysics: the legacy couplings, or the physical ones (play, give,
  breaking) of the player's train or of every train;
- physics.derailment: off, classic (the simulator's own monitor), physical;
- physics.collisions: classic or physical (the simulator's own);
- physics.trackbuckling: off, from the scenery (its own), also from heat;
- deer: animals by the track;
with the crash damage, the breaking pantographs, the power from the wires only
and the state of a consist at its start, moved here from General.

Two levels set them in one go: Arcade forgives - no derailment, the legacy
couplings and collisions, no buckling, no damage, power without the wires, every
consist ready to drive; Realistic takes the physical models - the couplings of
the player's train, the derailment by its criteria (a track's own speed limit
still derails nothing), the crush of the ends, buckling in the heat - and the
consists as the scenario has them. The simulator's own values stand between the
two and show as custom; each setting can still be changed on its own.

The developer tab has physics.derailment.trustlimits,
controlcoupling.enginecheck, ai.brakesetting.cw1 and physics.trackcheck. A list
takes the other words the simulator reads for its own (couplerphysics true,
physics.derailment no), and a word of a list is named per key where it means
something of its own there.
2026-11-29 20:16:24 +01:00

309 lines
12 KiB
C#

using Avalonia.Controls;
using StarterNG.Application;
using StarterNG.Controls;
using StarterNG.Tests.Fakes;
namespace StarterNG.Tests;
/// <summary>
/// Values of eu07.ini the starter does not know, or a control cannot show, as the settings page
/// shows them: as the file writes them, with a note, until the player sets them by hand - or the
/// move to the standard mode puts them right.
/// </summary>
[Collection(AvaloniaControls.Name)]
public class SettingsUnknownValueTests
{
// The standard mode already, unless a test moves to it.
private const string Standard = "starter.settings.standard yes\n";
private static void WithPage(string ini, Action<StarterNG.Views.Settings, AppServices, Func<string>> act)
{
var installation = new TestInstallation(new InMemoryFileSystem());
installation.Environment.AsWindows();
var services = AppServices.Initialize(installation.Paths, installation.Files, installation.Clock,
installation.Environment);
string path = services.SettingsPaths.UserConfigPath();
installation.Files.WriteAllText(path, ini);
services.SettingsStore.Load();
HeadlessDrawing.Ensure();
var context = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(new Avalonia.Threading.AvaloniaSynchronizationContext());
Window? window = null;
try
{
var page = new StarterNG.Views.Settings();
window = Controls.Show(page, 1600, 1000, themed: true);
act(page, services, () => installation.Files.ReadAllText(path).Replace("\r\n", "\n"));
}
finally
{
window?.Close();
SynchronizationContext.SetSynchronizationContext(context);
}
}
private static ComboBox List(StarterNG.Views.Settings page, string name) => page.FindControl<ComboBox>(name)!;
private static string? Shown(ComboBox list) => (list.SelectedItem as ComboBoxItem)?.Content as string;
private static SettingRow Row(StarterNG.Views.Settings page, string key) =>
Avalonia.LogicalTree.LogicalExtensions.GetLogicalDescendants(page).OfType<SettingRow>()
.First(row => row.ConfigKey == key);
private static string? Note(StarterNG.Views.Settings page, string key) =>
Avalonia.LogicalTree.LogicalExtensions.GetLogicalDescendants(page).OfType<SettingRow>()
.First(row => row.ConfigKey == key).UnknownValue;
[Fact]
public void A_renderer_word_is_on_the_list_chosen_and_saved_as_it_is()
{
WithPage(Standard + "gfxrenderer null\nfieldofview 45\n", (page, _, saved) =>
{
Assert.Equal("null", Shown(List(page, "RenderEngineCb")));
Assert.Contains("null", Note(page, "gfxrenderer"));
Assert.False(page.HasUnsavedChanges);
page.Save();
Assert.Contains("gfxrenderer null\n", saved());
});
}
[Fact]
public void The_standard_mode_has_the_choice_of_the_simulator_and_of_ray_tracing_but_not_of_the_renderer()
{
WithPage(Standard, (page, _, _) =>
{
Assert.True(Row(page, "starter.exe.path").IsEffectivelyVisible);
Assert.True(Row(page, "gfx.raytracing, gfx.raytracing.range").IsVisible);
Assert.False(Row(page, "gfxrenderer").IsVisible);
});
}
[Fact]
public void A_level_of_realism_picked_is_saved_and_marked_until_a_setting_moves()
{
WithPage(Standard, (page, services, saved) =>
{
var buttons = page.FindControl<StackPanel>("PhysicsPresetButtons")!.Children.OfType<Button>().ToList();
var state = page.FindControl<TextBlock>("PhysicsPresetState")!;
Assert.Equal(App.Loc["PresetCustom"], state.Text);
buttons[1].RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent));
Controls.Settle();
Assert.True(buttons[1].Classes.Contains("Accent"));
Assert.Equal("new", (((ComboBox)Row(page, "physics.derailment").Content!).SelectedItem as ComboBoxItem)?.Tag);
page.Save();
Assert.Contains("physics.derailment new\n", saved());
Assert.Contains("couplerphysics yes\n", saved());
Assert.Contains("crashdamage yes\n", saved());
page.FindControl<CheckBox>("CrashDamageCb")!.IsChecked = false;
Controls.Settle();
Assert.Equal(App.Loc["PresetCustom"], state.Text);
Assert.False(buttons[1].Classes.Contains("Accent"));
});
}
[Fact]
public void A_level_of_ray_tracing_picked_is_saved_as_its_reach()
{
WithPage(Standard, (page, services, saved) =>
{
var list = List(page, "RayTracingCb");
Assert.Equal(App.Loc["RayTracingOff"], Shown(list));
list.SelectedIndex = 3;
page.Save();
Assert.Contains("gfx.raytracing yes\n", saved());
Assert.Contains("gfx.raytracing.range 1000\n", saved());
list.SelectedIndex = 0;
page.Save();
Assert.Contains("gfx.raytracing no\n", saved());
Assert.Contains("gfx.raytracing.range 1000\n", saved());
});
}
[Fact]
public void A_reach_of_ray_tracing_no_level_has_is_shown_as_it_is_and_kept()
{
WithPage(Standard + "gfx.raytracing yes\ngfx.raytracing.range 750\n", (page, _, saved) =>
{
Assert.Contains("750", Shown(List(page, "RayTracingCb")));
Assert.False(page.HasUnsavedChanges);
page.Save();
Assert.Contains("gfx.raytracing.range 750\n", saved());
});
}
[Fact]
public void Another_renderer_picked_from_the_list_replaces_it()
{
WithPage(Standard + "gfxrenderer null\n", (page, services, saved) =>
{
List(page, "RenderEngineCb").SelectedIndex = 1;
page.Save();
Assert.Contains("gfxrenderer default\n", saved());
Assert.Null(services.Settings.RendererWord);
Assert.Null(Note(page, "gfxrenderer"));
});
}
[Fact]
public void A_value_a_list_does_not_offer_is_listed_as_written_and_kept()
{
WithPage(Standard + "starter.batterydefault 7\ngfx.shadow.rank.cutoff 9\n", (page, _, saved) =>
{
Assert.Equal("7", Shown(List(page, "ReadyOverrideCb")));
Assert.Equal("9", Shown(List(page, "ShadowDisplayCb")));
Assert.Contains("7", Note(page, "starter.batterydefault"));
Assert.Contains("9", Note(page, "gfx.shadow.rank.cutoff"));
Assert.False(page.HasUnsavedChanges);
page.Save();
Assert.Contains("starter.batterydefault 7\n", saved());
Assert.Contains("gfx.shadow.rank.cutoff 9\n", saved());
});
}
[Fact]
public void Picking_the_value_the_starter_shows_by_default_replaces_the_files_own()
{
// "7" is read as the scenery's choice, the first on the list: picked by hand, it is written.
WithPage(Standard + "starter.batterydefault 7\n", (page, _, saved) =>
{
List(page, "ReadyOverrideCb").SelectedIndex = 0;
Assert.Null(Note(page, "starter.batterydefault"));
page.Save();
Assert.Contains("starter.batterydefault 0\n", saved());
});
}
[Fact]
public void A_list_put_back_at_the_files_value_keeps_it()
{
WithPage(Standard + "starter.batterydefault 7\n", (page, _, saved) =>
{
var list = List(page, "ReadyOverrideCb");
var fileValue = list.SelectedItem;
list.SelectedIndex = 1;
list.SelectedItem = fileValue;
page.Save();
Assert.Contains("starter.batterydefault 7\n", saved());
});
}
[Fact]
public void A_control_set_by_hand_to_the_default_replaces_a_value_it_could_not_read()
{
WithPage(Standard + "vsync moze\n", (page, _, saved) =>
{
Assert.Contains("moze", Note(page, "vsync"));
var box = page.FindControl<CheckBox>("VSyncCb")!;
box.IsChecked = true;
box.IsChecked = false;
page.Save();
Assert.Contains("vsync no\n", saved());
});
}
[Fact]
public void A_level_for_other_settings_leaves_a_value_of_the_file_alone()
{
WithPage(Standard + "starter.batterydefault 7\ngfxrenderer vulkan\n", (page, _, saved) =>
{
page.FindControl<Panel>("PresetButtons")!.Children.OfType<Button>().First()
.RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent));
Assert.Equal("7", Shown(List(page, "ReadyOverrideCb")));
page.Save();
Assert.Contains("starter.batterydefault 7\n", saved());
Assert.Contains("gfxrenderer vulkan\n", saved());
});
}
[Fact]
public void The_move_to_the_standard_mode_puts_values_of_the_file_right()
{
WithPage("gfxrenderer null\nstarter.batterydefault 7\nvsync moze\nstarter.gpu system\n", (page, _, saved) =>
{
page.MoveToStandardOnce();
string text = saved();
Assert.Contains("gfxrenderer vulkan\n", text);
Assert.Contains("starter.batterydefault 0\n", text);
Assert.Contains("vsync no\n", text);
// What lets some machines run the simulator at all stays.
Assert.Contains("starter.gpu system\n", text);
Assert.Null(Note(page, "gfxrenderer"));
Assert.NotEqual("7", Shown(List(page, "ReadyOverrideCb")));
});
}
// A player of the standard mode of an older starter: Vulkan, every optimisation on and the developer's settings
// at the starter's own values, the picture's level and what the player chose in the standard mode as it was.
[Fact]
public void The_standard_mode_of_an_older_layout_moves_to_vulkan_and_the_fast_choices()
{
WithPage(Standard + "gfxrenderer default\ngfx.gpucull no\nphysicslog yes\nfriction 2\nmaxtexturesize 1024\n" +
"fieldofview 60\ngfx.ssr low\nblackbox yes\n", (page, services, saved) =>
{
page.MoveToStandardOnce();
string text = saved();
Assert.Contains("gfxrenderer vulkan\n", text);
Assert.Contains("gfx.gpucull yes\n", text);
Assert.Contains("physicslog no\n", text);
Assert.Contains("friction 1\n", text);
Assert.Contains("maxtexturesize 1024\n", text);
Assert.Contains("gfx.ssr low\n", text);
Assert.Contains("fieldofview 60\n", text);
Assert.Contains("blackbox yes\n", text);
Assert.Contains($"starter.settings.revision {StarterNG.Domain.Settings.SimulatorSettings.CurrentRevision}\n", text);
});
}
[Fact]
public void The_advanced_mode_of_an_older_layout_keeps_what_its_developer_chose()
{
WithPage(Standard + "starter.settings.advanced yes\ngfxrenderer default\ngfx.gpucull no\n", (page, services, saved) =>
{
page.MoveToStandardOnce();
string text = saved();
Assert.Contains("gfxrenderer default\n", text);
Assert.Contains("gfx.gpucull no\n", text);
Assert.True(services.Settings.AdvancedSettings);
Assert.Equal(StarterNG.Domain.Settings.SimulatorSettings.CurrentRevision, services.Settings.Revision);
});
}
[Fact]
public void The_layout_of_this_starter_is_moved_to_once_only()
{
WithPage(Standard + "starter.settings.revision 2\ngfxrenderer default\n", (page, _, saved) =>
{
page.MoveToStandardOnce();
Assert.Contains("gfxrenderer default\n", saved());
});
}
}