Skip to content

Commit

Permalink
Simplify command implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
y-iihoshi committed Jan 13, 2024
1 parent cb5c504 commit 753ea63
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 218 deletions.
96 changes: 48 additions & 48 deletions ThScoreFileConverter.Tests/ViewModels/MainWindowViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,10 @@ public void TemplateFilesSelectionChangedCommandTest()
using var disposed = window.DeleteTemplateFilesCommand
.CanExecuteChangedAsObservable().Subscribe(_ => ++numChanged);

Assert.IsTrue(command.CanExecute());
Assert.IsTrue(command.CanExecute(null));
Assert.AreEqual(0, numChanged);

command.Execute();
command.Execute(null);
Assert.AreEqual(1, numChanged);
}

Expand Down Expand Up @@ -674,10 +674,10 @@ public void DeleteAllTemplateFilesCommandTest()
Assert.AreEqual(1, numChanged);
CollectionAssert.That.AreEqual(fileNames, window.TemplateFiles.Value);

Assert.IsTrue(command.CanExecute());
Assert.IsTrue(command.CanExecute(null));
Assert.AreEqual(1, numChanged);

command.Execute();
command.Execute(null);
Assert.AreEqual(2, numChanged);
Assert.AreEqual(0, window.TemplateFiles.Value.Count());
}
Expand All @@ -699,10 +699,10 @@ public void DeleteAllTemplateFilesCommandTestEmpty()
var numChanged = 0;
using var disposable = window.TemplateFiles.Subscribe(_ => ++numChanged);

Assert.IsFalse(command.CanExecute());
Assert.IsFalse(command.CanExecute(null));
Assert.AreEqual(0, numChanged);

command.Execute();
command.Execute(null);
Assert.AreEqual(1, numChanged);
Assert.AreEqual(0, window.TemplateFiles.Value.Count());
}
Expand Down Expand Up @@ -776,9 +776,9 @@ public void ConvertCommandTest()
var command = window.ConvertCommand;
Assert.IsNotNull(command);

Assert.IsFalse(command.CanExecute());
Assert.IsFalse(command.CanExecute(null));

command.Execute();
command.Execute(null);
Assert.IsTrue(window.IsIdle.Value);
Assert.AreEqual(string.Empty, window.Log.Value);
}
Expand All @@ -794,10 +794,10 @@ public void DraggingCommandTest()
var args = CreateDragEventArgs(
new DataObject(DataFormats.FileDrop, new object()), UIElement.PreviewDragEnterEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
Assert.AreEqual(DragDropEffects.Copy, args!.Effects);
command.Execute(args);
Assert.AreEqual(DragDropEffects.Copy, args.Effects);
Assert.IsTrue(args.Handled);
}

Expand All @@ -812,10 +812,10 @@ public void DraggingCommandTestNone()
var args = CreateDragEventArgs(
new DataObject(DataFormats.Text, new object()), UIElement.PreviewDragEnterEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
Assert.AreEqual(DragDropEffects.None, args!.Effects);
command.Execute(args);
Assert.AreEqual(DragDropEffects.None, args.Effects);
Assert.IsFalse(args.Handled);
}

Expand All @@ -835,9 +835,9 @@ public void DropScoreFileCommandTest()
{
var args = CreateDragEventArgs(new DataObject(DataFormats.FileDrop, fileNames), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(1, numChanged);
Assert.AreEqual(fileNames[0], window.ScoreFile.Value);
}
Expand All @@ -863,9 +863,9 @@ public void DropScoreFileCommandTestNonexistent()

var args = CreateDragEventArgs(new DataObject(DataFormats.FileDrop, fileNames), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(0, numChanged);
Assert.AreEqual(string.Empty, window.ScoreFile.Value);
}
Expand All @@ -883,9 +883,9 @@ public void DropScoreFileCommandTestInvalidData()

var args = CreateDragEventArgs(new DataObject(DataFormats.FileDrop, default(int)), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(0, numChanged);
Assert.AreEqual(string.Empty, window.ScoreFile.Value);
}
Expand All @@ -903,9 +903,9 @@ public void DropScoreFileCommandTestInvalidDataFormat()

var args = CreateDragEventArgs(new DataObject(DataFormats.Text, string.Empty), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(0, numChanged);
Assert.AreEqual(string.Empty, window.ScoreFile.Value);
}
Expand All @@ -932,9 +932,9 @@ public void DropBestShotDirectoryCommandTest()
{
var args = CreateDragEventArgs(new DataObject(DataFormats.FileDrop, dirNames), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(1, numChanged);
Assert.AreEqual(dirNames[0], window.BestShotDirectory.Value);
}
Expand All @@ -960,9 +960,9 @@ public void DropBestShotDirectoryCommandTestNonexistent()

var args = CreateDragEventArgs(new DataObject(DataFormats.FileDrop, dirNames), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(0, numChanged);
Assert.AreEqual(string.Empty, window.BestShotDirectory.Value);
}
Expand All @@ -980,9 +980,9 @@ public void DropBestShotDirectoryCommandTestInvalidData()

var args = CreateDragEventArgs(new DataObject(DataFormats.FileDrop, default(int)), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(0, numChanged);
Assert.AreEqual(string.Empty, window.BestShotDirectory.Value);
}
Expand All @@ -1000,9 +1000,9 @@ public void DropBestShotDirectoryCommandTestInvalidDataFormat()

var args = CreateDragEventArgs(new DataObject(DataFormats.Text, string.Empty), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(0, numChanged);
Assert.AreEqual(string.Empty, window.BestShotDirectory.Value);
}
Expand All @@ -1025,9 +1025,9 @@ public void DropTemplateFilesCommandTest()
new DataObject(DataFormats.FileDrop, fileNames.Append("nonexistent.txt").ToArray()),
UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(1, numChanged);
CollectionAssert.That.AreEqual(fileNames, window.TemplateFiles.Value);
}
Expand All @@ -1051,9 +1051,9 @@ public void DropTemplateFilesCommandTestInvalidData()

var args = CreateDragEventArgs(new DataObject(DataFormats.FileDrop, default(int)), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(0, numChanged);
Assert.AreEqual(0, window.TemplateFiles.Value.Count());
}
Expand All @@ -1071,9 +1071,9 @@ public void DropTemplateFilesCommandTestInvalidDataFormat()

var args = CreateDragEventArgs(new DataObject(DataFormats.Text, string.Empty), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(0, numChanged);
Assert.AreEqual(0, window.TemplateFiles.Value.Count());
}
Expand All @@ -1100,9 +1100,9 @@ public void DropOutputDirectoryCommandTest()
{
var args = CreateDragEventArgs(new DataObject(DataFormats.FileDrop, dirNames), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(1, numChanged);
Assert.AreEqual(dirNames[0], window.OutputDirectory.Value);
}
Expand All @@ -1128,9 +1128,9 @@ public void DropOutputDirectoryCommandTestNonexistent()

var args = CreateDragEventArgs(new DataObject(DataFormats.FileDrop, dirNames), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(0, numChanged);
Assert.AreEqual(string.Empty, window.OutputDirectory.Value);
}
Expand All @@ -1148,9 +1148,9 @@ public void DropOutputDirectoryCommandTestInvalidData()

var args = CreateDragEventArgs(new DataObject(DataFormats.FileDrop, default(int)), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(0, numChanged);
Assert.AreEqual(string.Empty, window.OutputDirectory.Value);
}
Expand All @@ -1168,9 +1168,9 @@ public void DropOutputDirectoryCommandTestInvalidDataFormat()

var args = CreateDragEventArgs(new DataObject(DataFormats.Text, string.Empty), UIElement.DropEvent);
Assert.IsNotNull(args);
Assert.IsTrue(command.CanExecute(args!));
Assert.IsTrue(command.CanExecute(args));

command.Execute(args!);
command.Execute(args);
Assert.AreEqual(0, numChanged);
Assert.AreEqual(string.Empty, window.OutputDirectory.Value);
}
Expand All @@ -1189,9 +1189,9 @@ public void OpenAboutWindowCommandTest()
var command = window.OpenAboutWindowCommand;
Assert.IsNotNull(command);

Assert.IsTrue(command.CanExecute());
Assert.IsTrue(command.CanExecute(null));

command.Execute();
command.Execute(null);
dialogServiceMock.Received().ShowDialog(
nameof(AboutWindowViewModel), Arg.Any<DialogParameters>(), Arg.Any<Action<IDialogResult>>());
}
Expand All @@ -1210,9 +1210,9 @@ public void OpenSettingWindowCommandTest()
var command = window.OpenSettingWindowCommand;
Assert.IsNotNull(command);

Assert.IsTrue(command.CanExecute());
Assert.IsTrue(command.CanExecute(null));

command.Execute();
command.Execute(null);
dialogServiceMock.Received().ShowDialog(
nameof(SettingWindowViewModel), Arg.Any<DialogParameters>(), Arg.Any<Action<IDialogResult>>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ public void FontDialogCancelCommandTestNull()
var numChanged = 0;
using var disposable = window.ObserveProperty(w => w.Font, false).Subscribe(_ => ++numChanged);

Assert.IsTrue(command.CanExecute(null!));
Assert.IsTrue(command.CanExecute(null));
Assert.AreEqual(0, numChanged);

_ = Assert.ThrowsException<NullReferenceException>(() => command.Execute(null!));
_ = Assert.ThrowsException<NullReferenceException>(() => command.Execute(null));
}

[TestMethod]
Expand All @@ -380,10 +380,10 @@ public void ResetFontCommandTest()
var numChanged = 0;
using var _1 = window.ObserveProperty(w => w.Font, false).Subscribe(_ => ++numChanged);

Assert.IsTrue(command.CanExecute());
Assert.IsTrue(command.CanExecute(null));
Assert.AreEqual(0, numChanged);

command.Execute();
command.Execute(null);
Assert.AreEqual(1, numChanged);
adapterMock.Received(1).UpdateResources(SystemFonts.MessageFontFamily, SystemFonts.MessageFontSize);
}
Expand All @@ -401,10 +401,10 @@ public void ResetFontCommandTestDisposed()

window.Dispose();

Assert.IsTrue(command.CanExecute());
Assert.IsTrue(command.CanExecute(null));
Assert.AreEqual(0, numChanged);

_ = Assert.ThrowsException<ObjectDisposedException>(command.Execute);
_ = Assert.ThrowsException<ObjectDisposedException>(() => command.Execute(null));
}

[TestMethod]
Expand Down
1 change: 1 addition & 0 deletions ThScoreFileConverter/ThScoreFileConverter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="DependencyPropertyGenerator" Version="1.3.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
4 changes: 2 additions & 2 deletions ThScoreFileConverter/ViewModels/AboutWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Reflection;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Prism.Mvvm;
using CommunityToolkit.Mvvm.ComponentModel;
using Prism.Services.Dialogs;
using ThScoreFileConverter.Core.Resources;
using ThScoreFileConverter.Models;
Expand All @@ -24,7 +24,7 @@ namespace ThScoreFileConverter.ViewModels;
#if !DEBUG
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1812", Justification = "Instantiated by the DI container.")]
#endif
internal sealed class AboutWindowViewModel : BindableBase, IDialogAware
internal sealed class AboutWindowViewModel : ObservableObject, IDialogAware
{
/// <summary>
/// Initializes a new instance of the <see cref="AboutWindowViewModel"/> class.
Expand Down
Loading

0 comments on commit 753ea63

Please sign in to comment.