Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/RatioMaster.App.Tests/RatioMaster.App.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>RatioMaster.App.Tests</RootNamespace>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<OutputType>Exe</OutputType>
<UseVSTest>false</UseVSTest>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="xunit.v3" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\RatioMaster.App\RatioMaster.App.csproj" />
</ItemGroup>
</Project>
116 changes: 116 additions & 0 deletions src/RatioMaster.App.Tests/ViewModels/TorrentSettingsViewModelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
namespace RatioMaster.App.Tests.ViewModels
{
using RatioMaster.App.ViewModels;
using RatioMaster.Core.Clients;
using Xunit;

public sealed class TorrentSettingsViewModelTests
{
[Fact]
public void ClearingABaseSpeedPreventsSavingUntilCorrected()
{
var editor = CreateEditor();
Assert.True(editor.IsSpeedSettingsValid);

editor.UploadKb = null;
Assert.False(editor.IsSpeedSettingsValid);
Assert.NotNull(editor.SpeedValidationMessage);

editor.UploadKb = 0m;
Assert.True(editor.IsSpeedSettingsValid);
Assert.Null(editor.SpeedValidationMessage);
}

[Fact]
public void ClearingAnEnabledRandomRangePreventsSaving()
{
var editor = CreateEditor();
editor.NextUpdateRandomUpload = true;
editor.NextUpdateUploadMinKb = null;

Assert.False(editor.IsSpeedSettingsValid);

editor.NextUpdateUploadMinKb = 10m;
Assert.True(editor.IsSpeedSettingsValid);
}

[Fact]
public void ReversedRangeIsRejectedButIgnoredWhenDisabled()
{
var editor = CreateEditor();
editor.NextUpdateRandomDownload = true;
editor.NextUpdateDownloadMinKb = 70m;
editor.NextUpdateDownloadMaxKb = 60m;

Assert.False(editor.IsSpeedSettingsValid);
Assert.Contains("minimum", editor.SpeedValidationMessage!);

editor.NextUpdateRandomDownload = false;
Assert.True(editor.IsSpeedSettingsValid);
}

[Fact]
public void NonIntegralRandomRangeIsRejected()
{
var editor = CreateEditor();
editor.UploadRandomMinKb = 1.5m;

Assert.False(editor.IsSpeedSettingsValid);

editor.UploadRandomMinKb = 2m;
Assert.True(editor.IsSpeedSettingsValid);
}

[Fact]
public void ChangedValuesAreSerializedWithoutLosingPrecision()
{
var editor = CreateEditor();
editor.UploadKb = 12.5m;
editor.DownloadKb = 3.25m;
editor.NextUpdateRandomUpload = true;
editor.NextUpdateUploadMinKb = 15m;
editor.NextUpdateUploadMaxKb = 30m;

var settings = editor.ToSettings();

Assert.Equal(12800, settings.UploadRateBytes);
Assert.Equal(3328, settings.DownloadRateBytes);
Assert.Equal(15, settings.NextUpdateUploadMinKb);
Assert.Equal(30, settings.NextUpdateUploadMaxKb);
}

[Fact]
public void ModelDoesNotOverflowWhenADisabledRangeContainsAnOutOfRangeValue()
{
var editor = CreateEditor();
editor.UploadRandomEnabled = false;
editor.UploadRandomMaxKb = decimal.MaxValue;

Assert.True(editor.IsSpeedSettingsValid);
Assert.Equal(int.MaxValue, editor.ToSettings().UploadRandomMaxKb);
}

[Fact]
public void RangeValidationRaisesPropertyChangeNotifications()
{
var editor = CreateEditor();
var raised = 0;
editor.PropertyChanged += (_, args) =>
{
if (args.PropertyName == nameof(TorrentSettingsViewModel.IsSpeedSettingsValid))
{
raised++;
}
};

editor.UploadRandomMaxKb = 0m;
Assert.False(editor.IsSpeedSettingsValid);

editor.UploadRandomMaxKb = 10m;
Assert.True(editor.IsSpeedSettingsValid);
Assert.Equal(2, raised);
}

private static TorrentSettingsViewModel CreateEditor() => new(ClientProfileCatalog.Load());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public AddTorrentViewModel(
this.files = files;
this.defaults = defaults;
this.startImmediately = startImmediately;
this.Settings.PropertyChanged += (_, args) =>
{
if (args.PropertyName == nameof(TorrentSettingsViewModel.IsSpeedSettingsValid))
{
this.AddCommand.NotifyCanExecuteChanged();
}
};
}

public TorrentSettingsViewModel Settings { get; }
Expand All @@ -71,7 +78,7 @@ public AddTorrentViewModel(
_ => string.Format(CultureInfo.CurrentCulture, Strings.AddManyTorrents, this.Torrents.Count),
};

public bool CanAdd => this.Torrents.Count > 0;
public bool CanAdd => this.Torrents.Count > 0 && this.Settings.IsSpeedSettingsValid;

/// <summary>
/// Gets the reason the chosen tracker cannot be announced to (no tracker, udp://, malformed), or null when it is fine.
Expand Down
11 changes: 10 additions & 1 deletion src/RatioMaster.App/ViewModels/Dialogs/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public SettingsViewModel(AppSettings settings, TorrentSettingsViewModel defaults
this.Title = Strings.Settings;
this.original = settings;
this.Defaults = defaults;
this.Defaults.PropertyChanged += (_, args) =>
{
if (args.PropertyName == nameof(TorrentSettingsViewModel.IsSpeedSettingsValid))
{
this.SaveCommand.NotifyCanExecuteChanged();
}
};
this.theme = settings.Theme;
this.use24HourTime = settings.Use24HourTime;
this.checkForUpdatesOnStartup = settings.CheckForUpdatesOnStartup;
Expand Down Expand Up @@ -94,7 +101,9 @@ public SettingsViewModel(AppSettings settings, TorrentSettingsViewModel defaults

public string ClientSummary => string.Format(CultureInfo.CurrentCulture, Strings.ClientsSummary, this.ClientCount);

[RelayCommand]
public bool CanSave => this.Defaults.IsSpeedSettingsValid;

[RelayCommand(CanExecute = nameof(CanSave))]
private void Save() => this.Close(this.original with
{
Theme = this.Theme,
Expand Down
23 changes: 19 additions & 4 deletions src/RatioMaster.App/ViewModels/TorrentItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ public TorrentItemViewModel(
this.Log = new LogViewModel(displayName, files, clipboard);
this.Tracker = new TrackerHistoryViewModel();
this.Settings = new TorrentSettingsViewModel(factory.Catalog, factory, settings);
this.Settings.PropertyChanged += (_, args) =>
{
if (args.PropertyName == nameof(TorrentSettingsViewModel.IsSpeedSettingsValid))
{
this.StartCommand.NotifyCanExecuteChanged();
this.ApplySettingsCommand.NotifyCanExecuteChanged();
}
};
}

/// <summary>Raised when the row wants to be removed from the list.</summary>
Expand Down Expand Up @@ -154,7 +162,7 @@ public TorrentItemViewModel(
public bool IsRunning => this.State is TorrentSessionState.Starting or TorrentSessionState.Downloading
or TorrentSessionState.Seeding or TorrentSessionState.Updating;

public bool CanStart => !this.IsRunning && this.State != TorrentSessionState.Stopping;
public bool CanStart => !this.IsRunning && this.State != TorrentSessionState.Stopping && this.Settings.IsSpeedSettingsValid;

public string StatusText => this.State switch
{
Expand Down Expand Up @@ -234,6 +242,11 @@ public void Refresh()
[RelayCommand(CanExecute = nameof(CanStart))]
public void Start()
{
if (!this.CanStart)
{
return;
}

// A session that stopped on its own (a stop condition, a tracker error) since the last refresh may have got
// further than that refresh saw, up to complete: carry on from where it really got to.
if (this.session is { } previous)
Expand Down Expand Up @@ -280,7 +293,9 @@ public async Task OpenContainingFolderAsync()
public void Remove() => this.RemoveRequested?.Invoke(this, EventArgs.Empty);

/// <summary>Pushes the editor's values into the settings, and into a running session where allowed.</summary>
[RelayCommand]
public bool CanApplySettings => this.Settings.IsSpeedSettingsValid;

[RelayCommand(CanExecute = nameof(CanApplySettings))]
public void ApplySettings()
{
this.settings = this.Settings.ToSettings();
Expand Down Expand Up @@ -330,13 +345,13 @@ public void SetSpeeds(double? uploadKb, double? downloadKb)
if (uploadKb is { } up)
{
settings = settings with { UploadRateBytes = (long)(up * 1024) };
this.Settings.UploadKb = up;
this.Settings.UploadKb = (decimal)up;
}

if (downloadKb is { } down)
{
settings = settings with { DownloadRateBytes = (long)(down * 1024) };
this.Settings.DownloadKb = down;
this.Settings.DownloadKb = (decimal)down;
}

this.settings = settings;
Expand Down
Loading