Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

CDT-84: Fix Open File Dialog gives keyboard focus to main window on c…

…lose

New Profile from Edit Profile screen
Fix Escape closing both parent/child dialog windows
Fix window owner issues
Add ShowDialogWindow, ShowOpenFileDialog to ViewModelBase
Add bool? parameter to ViewModelBase's CloseWindow for setting the Window's DialogResult
  • Loading branch information...
commit d30f4202b5fe7b3660233fd1beab89f6612b230d 1 parent 138d1a4
@judwhite authored
View
15 CDTag.Common/ApplicationServices/IDialogService.cs
@@ -1,4 +1,5 @@
using System;
+using System.Windows;
using CDTag.Common.Wpf;
using CDTag.Views.Interfaces;
@@ -19,9 +20,16 @@ public interface IDialogService
void ShowError(Exception exception, IErrorContainer errorContainer);
/// <summary>Shows a window.</summary>
+ /// <param name="window">The window.</param>
+ /// <param name="owner">The owner.</param>
+ /// <returns>The result of <see cref="IWindow.ShowDialog()"/>.</returns>
+ bool? ShowWindow(IWindow window, Window owner);
+
+ /// <summary>Shows a window.</summary>
/// <typeparam name="T">The window type.</typeparam>
- /// <returns>The result of <see cref="IWindow.ShowDialog()" />.</returns>
- bool? ShowWindow<T>()
+ /// <param name="owner">The owner.</param>
+ /// <returns>The result of <see cref="IWindow.ShowDialog()"/>.</returns>
+ bool? ShowWindow<T>(Window owner)
where T : IWindow;
/// <summary>Closes the address bar text box.</summary>
@@ -30,8 +38,9 @@ public interface IDialogService
/// <summary>Shows the open file dialog.</summary>
/// <param name="title">The dialog title.</param>
/// <param name="filter">The file filter.</param>
+ /// <param name="owner">The owner.</param>
/// <param name="fileName">Name of the file opened.</param>
/// <returns><c>true</c> if a file is selected.</returns>
- bool? ShowOpenFileDialog(string title, string filter, out string fileName);
+ bool? ShowOpenFileDialog(string title, string filter, Window owner, out string fileName);
}
}
View
2  CDTag.Common/CDTag.Common.csproj
@@ -61,6 +61,8 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Events\ShowOpenFileDialogEvent.cs" />
+ <Compile Include="Events\ShowWindowEvent.cs" />
<Compile Include="Hash\CRC32.cs" />
<Compile Include="Hash\MD5.cs" />
<Compile Include="Hash\SHA1.cs" />
View
24 CDTag.Common/Events/ShowOpenFileDialogEvent.cs
@@ -0,0 +1,24 @@
+namespace CDTag.Common.Events
+{
+ /// <summary>
+ /// ShowOpenFileDialogEvent
+ /// </summary>
+ public class ShowOpenFileDialogEvent
+ {
+ /// <summary>Gets or sets the title.</summary>
+ /// <value>The title.</value>
+ public string Title { get; set; }
+
+ /// <summary>Gets or sets the filter.</summary>
+ /// <value>The filter.</value>
+ public string Filter { get; set; }
+
+ /// <summary>Gets or sets the name of the file.</summary>
+ /// <value>The name of the file.</value>
+ public string FileName { get; set; }
+
+ /// <summary>Gets or sets the result.</summary>
+ /// <value>The result.</value>
+ public bool? Result { get; set; }
+ }
+}
View
18 CDTag.Common/Events/ShowWindowEvent.cs
@@ -0,0 +1,18 @@
+using CDTag.Views.Interfaces;
+
+namespace CDTag.Common.Events
+{
+ /// <summary>
+ /// ShowWindowEvent
+ /// </summary>
+ public class ShowWindowEvent
+ {
+ /// <summary>Gets or sets the type of the window. Must inherit <see cref="IWindow" />.</summary>
+ /// <value>The type of the window.</value>
+ public IWindow IWindow { get; set; }
+
+ /// <summary>Gets or sets the ShowDialog result.</summary>
+ /// <value>The ShowDialog result.</value>
+ public bool? Result { get; set; }
+ }
+}
View
10 CDTag.Common/MVVM/IViewModelBase.cs
@@ -12,16 +12,22 @@ namespace CDTag.Common.Mvvm
/// </summary>
public interface IViewModelBase : INotifyPropertyChanged
{
- /// <summary>Occurs when <see cref="ShowMessageBox" /> has been called.</summary>
+ /// <summary>Occurs when MessageBox /> has been called.</summary>
event EventHandler<DataEventArgs<MessageBoxEvent>> ShowMessageBox;
+ /// <summary>Occurs when ShowWindow has been called.</summary>
+ event EventHandler<DataEventArgs<ShowWindowEvent>> ShowDialogWindow;
+
+ /// <summary>Occurs when ShowOpenFileDialog has been called.</summary>
+ event EventHandler<DataEventArgs<ShowOpenFileDialogEvent>> ShowOpenFile;
+
/// <summary>Gets or sets the error container.</summary>
/// <value>The error container.</value>
IErrorContainer ErrorContainer { get; set; }
/// <summary>Gets or sets the close window action.</summary>
/// <value>The close window action.</value>
- Action CloseWindow { get; set; }
+ Action<bool?> CloseWindow { get; set; }
/// <summary>Gets or sets the current visual state.</summary>
/// <value>The current visual state.</value>
View
58 CDTag.Common/MVVM/ViewModelBase.cs
@@ -7,6 +7,7 @@
using CDTag.Common.Dispatcher;
using CDTag.Common.Events;
using CDTag.Common.Wpf;
+using CDTag.Views.Interfaces;
using IdSharp.Common.Events;
namespace CDTag.Common.Mvvm
@@ -58,9 +59,15 @@ public abstract class ViewModelBase : IViewModelBase
/// <summary>Occurs when a property value changes.</summary>
public event PropertyChangedEventHandler PropertyChanged;
- /// <summary>Occurs when <see cref="ShowMessageBox"/> has been called.</summary>
+ /// <summary>Occurs when MessageBox has been called.</summary>
public event EventHandler<DataEventArgs<MessageBoxEvent>> ShowMessageBox;
+ /// <summary>Occurs when ShowWindow has been called.</summary>
+ public event EventHandler<DataEventArgs<ShowWindowEvent>> ShowDialogWindow;
+
+ /// <summary>Occurs when ShowOpenFileDialog has been called.</summary>
+ public event EventHandler<DataEventArgs<ShowOpenFileDialogEvent>> ShowOpenFile;
+
private readonly Dictionary<string, object> _propertyValues = new Dictionary<string, object>();
/// <summary>The event aggregator.</summary>
@@ -89,7 +96,7 @@ protected ViewModelBase(IEventAggregator eventAggregator)
/// Gets or sets the close window action.
/// </summary>
/// <value>The close window action.</value>
- public Action CloseWindow { get; set; }
+ public Action<bool?> CloseWindow { get; set; }
/// <summary>Gets or sets the error container.</summary>
/// <value>The error container.</value>
@@ -254,5 +261,52 @@ protected MessageBoxResult MessageBox(string messageBoxText, string caption, Mes
return MessageBox(messageBox);
}
+
+ /// <summary>Shows a window.</summary>
+ /// <typeparam name="T">The window type.</typeparam>
+ /// <returns>The result of <see cref="IWindow.ShowDialog()" />.</returns>
+ protected bool? ShowWindow<T>()
+ where T : IWindow
+ {
+ ShowWindowEvent showWindowEvent = new ShowWindowEvent
+ {
+ IWindow = IoC.Resolve<T>(),
+ };
+
+ var handler = ShowDialogWindow;
+ if (handler != null)
+ handler(this, new DataEventArgs<ShowWindowEvent>(showWindowEvent));
+ else
+ throw new Exception("'ShowDialogWindow' event is not subscribed to.");
+
+ return showWindowEvent.Result;
+ }
+
+ /// <summary>Shows the open file dialog.</summary>
+ /// <param name="title">The dialog title.</param>
+ /// <param name="filter">The file filter.</param>
+ /// <param name="fileName">Name of the file opened.</param>
+ /// <returns><c>true</c> if a file is selected.</returns>
+ protected bool? ShowOpenFileDialog(string title, string filter, out string fileName)
+ {
+ ShowOpenFileDialogEvent showOpenFileDialogEvent = new ShowOpenFileDialogEvent
+ {
+ Title = title,
+ Filter = filter
+ };
+
+ var handler = ShowOpenFile;
+ if (handler != null)
+ handler(this, new DataEventArgs<ShowOpenFileDialogEvent>(showOpenFileDialogEvent));
+ else
+ throw new Exception("'ShowOpenFile' event is not subscribed to.");
+
+ if (showOpenFileDialogEvent.Result == true)
+ fileName = showOpenFileDialogEvent.FileName;
+ else
+ fileName = null;
+
+ return showOpenFileDialogEvent.Result;
+ }
}
}
View
4 CDTag.FileBrowser/CDTag.FileBrowser.csproj
@@ -140,6 +140,10 @@
<Project>{5D4DFAF4-4F2D-4713-9F96-7CAB7759E34C}</Project>
<Name>CDTag.Common</Name>
</ProjectReference>
+ <ProjectReference Include="..\CDTag.Views.Interfaces\CDTag.Views.Interfaces.csproj">
+ <Project>{B28B82E3-F2AF-455D-A309-C2CE687FD530}</Project>
+ <Name>CDTag.Views.Interfaces</Name>
+ </ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
View
61 CDTag.ViewModels/Profile/EditProfile/EditProfileViewModel.cs
@@ -9,6 +9,7 @@
using CDTag.Common.Events;
using CDTag.Common.Mvvm;
using CDTag.Model.Profile;
+using CDTag.Views.Interfaces.Profile.NewProfile;
namespace CDTag.ViewModels.Profile.EditProfile
{
@@ -134,7 +135,63 @@ public ICommand DeleteProfileCommand
private void NewProfile()
{
- throw new NotImplementedException();
+ var currentProfile = Profile;
+ if (currentProfile != null && currentProfile.HasChanges)
+ {
+ var msgBoxResult = MessageBox(string.Format("Do you want to save changes to {0}?", currentProfile.ProfileName), "Save changes", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
+ if (msgBoxResult == MessageBoxResult.Cancel)
+ return;
+ if (msgBoxResult == MessageBoxResult.Yes)
+ {
+ currentProfile.Save();
+ }
+ }
+
+ bool? result = ShowWindow<INewProfileWindow>();
+
+ if (result == true)
+ {
+ // TODO: What if the user overwrites an existing profile?
+
+ UserProfile newProfile = null;
+
+ string[] files = Directory.GetFiles(_pathService.ProfileDirectory, "*.cfg").OrderBy(p => p.ToLower()).ToArray();
+ foreach (string file in files)
+ {
+ bool exists = false;
+ foreach (var profile in Profiles)
+ {
+ if (string.Compare(profile.GetProfileFileName(), file, ignoreCase: true) == 0)
+ {
+ exists = true;
+ break;
+ }
+ }
+ if (exists)
+ {
+ continue;
+ }
+
+ try
+ {
+ var profile = UserProfile.Load(file);
+ _profiles.Add(profile);
+
+ if (newProfile == null)
+ newProfile = profile;
+ }
+ catch (Exception ex)
+ {
+ // TODO
+ Debug.WriteLine(ex);
+ }
+ }
+
+ if (newProfile != null)
+ {
+ Profile = newProfile;
+ }
+ }
}
private void RenameProfile()
@@ -153,7 +210,7 @@ private void DeleteProfile()
if (profile == null)
return;
- string messageBoxText = string.Format("Do you want to delete the {0} profile?", profile.ProfileName);
+ string messageBoxText = string.Format("Do you want to delete {0}?", profile.ProfileName);
if (_profiles.Count == 1)
{
messageBoxText += string.Format("{0}{0}If you delete this profile a Default profile will be created.", Environment.NewLine);
View
5 CDTag.ViewModels/Profile/NewProfile/NewProfileViewModel.cs
@@ -289,9 +289,8 @@ private void Next()
}
else
{
- IDialogService dialogService = IoC.Resolve<IDialogService>();
string fileName;
- bool? result = dialogService.ShowOpenFileDialog(title: "Open file", filter: "*.nfo|*.nfo", fileName: out fileName);
+ bool? result = ShowOpenFileDialog(title: "Open file", filter: "*.nfo|*.nfo", fileName: out fileName);
if (result != true)
return;
@@ -307,7 +306,7 @@ private void Next()
Profile.Save();
- CloseWindow(); // TODO: Open Edit Profile
+ CloseWindow(true); // TODO: Open Edit Profile
}
}
View
2  CDTag.ViewModels/Tag/TagAlbum/TagAlbumViewModel.cs
@@ -41,7 +41,7 @@ private void Finish()
{
Album.Finish();
- CloseWindow();
+ CloseWindow(true);
}
private void PreviewNFO()
View
26 CDTag.ViewModels/Tag/TagViewModel.cs
@@ -23,21 +23,19 @@ public partial class TagViewModel : ViewModelBase<ITagViewModel>, ITagViewModel
public TagViewModel(IEventAggregator eventAggregator)
: base(eventAggregator)
{
- IDialogService app = IoC.Resolve<IDialogService>();
-
- AboutCommand = new DelegateCommand(() => app.ShowWindow<IAboutWindow>());
+ AboutCommand = new DelegateCommand(() => ShowWindow<IAboutWindow>());
ExitCommand = new DelegateCommand(() => Application.Current.MainWindow.Close());
- TagAlbumCommand = new DelegateCommand(() => app.ShowWindow<ITagAlbumWindow>());
- EditTagsCommand = new DelegateCommand(() => app.ShowWindow<IEditTagWindow>());
- MassTagCommand = new DelegateCommand(() => app.ShowWindow<IMassTagWindow>());
- NewProfileCommand = new DelegateCommand(() => app.ShowWindow<INewProfileWindow>());
- EditProfileCommand = new DelegateCommand(() => app.ShowWindow<IEditProfileWindow>());
- SplitCueSheetCommand = new DelegateCommand(() => app.ShowWindow<ISplitCueWindow>());
- EncodingInspectorCommand = new DelegateCommand(() => app.ShowWindow<IEncodingInspectorWindow>());
- OptionsCommand = new DelegateCommand(() => app.ShowWindow<IOptionsWindow>());
- CreateChecksumCommand = new DelegateCommand(() => app.ShowWindow<IChecksumWindow>());
- VerifyChecksumCommand = new DelegateCommand(() => app.ShowWindow<IChecksumWindow>());
- VerifyEACLogCommand = new DelegateCommand(() => app.ShowWindow<IVerifyEACLogWindow>());
+ TagAlbumCommand = new DelegateCommand(() => ShowWindow<ITagAlbumWindow>());
+ EditTagsCommand = new DelegateCommand(() => ShowWindow<IEditTagWindow>());
+ MassTagCommand = new DelegateCommand(() => ShowWindow<IMassTagWindow>());
+ NewProfileCommand = new DelegateCommand(() => ShowWindow<INewProfileWindow>());
+ EditProfileCommand = new DelegateCommand(() => ShowWindow<IEditProfileWindow>());
+ SplitCueSheetCommand = new DelegateCommand(() => ShowWindow<ISplitCueWindow>());
+ EncodingInspectorCommand = new DelegateCommand(() => ShowWindow<IEncodingInspectorWindow>());
+ OptionsCommand = new DelegateCommand(() => ShowWindow<IOptionsWindow>());
+ CreateChecksumCommand = new DelegateCommand(() => ShowWindow<IChecksumWindow>());
+ VerifyChecksumCommand = new DelegateCommand(() => ShowWindow<IChecksumWindow>());
+ VerifyEACLogCommand = new DelegateCommand(() => ShowWindow<IVerifyEACLogWindow>());
EnhancedPropertyChanged += TagViewModel_EnhancedPropertyChanged;
View
17 CDTag.Views/DialogService.cs
@@ -22,18 +22,21 @@ static DialogService()
_dispatcher = IoC.Resolve<IDispatcher>();
}
- public bool? ShowWindow<T>()
+ public bool? ShowWindow<T>(Window owner)
where T : IWindow
{
- T window;
+ return ShowWindow(IoC.Resolve<T>(), owner);
+ }
+
+ public bool? ShowWindow(IWindow window, Window owner)
+ {
MouseHelper.SetWaitCursor();
try
{
- window = IoC.Resolve<T>();
- window.Owner = Application.Current.MainWindow; // TODO ?
+ window.Owner = owner;
var viewModel = window.DataContext as IViewModelBase;
if (viewModel != null)
- viewModel.CloseWindow = () => window.Close();
+ viewModel.CloseWindow = (result) => { ((Window)window).DialogResult = result; window.Close(); };
}
finally
{
@@ -77,7 +80,7 @@ public void CloseAddressTextBox()
IoC.Resolve<IEventAggregator>().Publish<CloseAddressTextBoxEvent>(null);
}
- public bool? ShowOpenFileDialog(string title, string filter, out string fileName)
+ public bool? ShowOpenFileDialog(string title, string filter, Window owner, out string fileName)
{
OpenFileDialog openFileDialog;
MouseHelper.SetWaitCursor();
@@ -92,7 +95,7 @@ public void CloseAddressTextBox()
MouseHelper.ResetCursor();
}
- bool? result = openFileDialog.ShowDialog(Application.Current.MainWindow);
+ bool? result = openFileDialog.ShowDialog(owner);
if (result == true)
fileName = openFileDialog.FileName;
View
4 CDTag.Views/ViewBase.cs
@@ -1,6 +1,4 @@
-using System;
-using System.Windows.Controls;
-using CDTag.Common;
+using System.Windows.Controls;
using CDTag.Common.Mvvm;
namespace CDTag.Views
View
33 CDTag.Views/WindowViewBase.cs
@@ -3,6 +3,8 @@
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
+using CDTag.Common;
+using CDTag.Common.ApplicationServices;
using CDTag.Common.Events;
using CDTag.Common.Mvvm;
using CDTag.Common.Settings;
@@ -25,6 +27,13 @@ public class WindowViewBase : Window, IWindow
private readonly IViewModelBase _viewModel;
private bool _settingsLoaded;
+ private static readonly IDialogService _dialogService;
+
+ static WindowViewBase()
+ {
+ _dialogService = IoC.Resolve<IDialogService>();
+ }
+
/// <summary>
/// Initializes a new instance of the <see cref="WindowViewBase"/> class.
/// </summary>
@@ -48,6 +57,29 @@ protected WindowViewBase(IViewModelBase viewModel)
Background = (Brush)Application.Current.Resources["WindowBackground"];
_viewModel.ShowMessageBox += viewModel_ShowMessageBox;
+ _viewModel.ShowDialogWindow += viewModel_ShowDialogWindow;
+ _viewModel.ShowOpenFile += viewModel_ShowOpenFile;
+ }
+
+ private void viewModel_ShowOpenFile(object sender, DataEventArgs<ShowOpenFileDialogEvent> e)
+ {
+ if (e == null)
+ throw new ArgumentNullException("e");
+
+ var ofd = e.Data;
+ string fileName;
+ bool? result = _dialogService.ShowOpenFileDialog(ofd.Title, ofd.Filter, this, out fileName);
+ ofd.FileName = fileName;
+ ofd.Result = result;
+ }
+
+ private void viewModel_ShowDialogWindow(object sender, DataEventArgs<ShowWindowEvent> e)
+ {
+ if (e == null)
+ throw new ArgumentNullException("e");
+
+ bool? result = _dialogService.ShowWindow(e.Data.IWindow, this);
+ e.Data.Result = result;
}
private void viewModel_ShowMessageBox(object sender, DataEventArgs<MessageBoxEvent> e)
@@ -173,6 +205,7 @@ private void WindowViewBase_PreviewKeyDown(object sender, KeyEventArgs e)
if (e.Key == Key.Escape)
{
Close();
+ e.Handled = true;
}
}
}
View
2  Tests/CDTag.ViewModels.Tests/CDTag.ViewModels.Tests.csproj
@@ -39,6 +39,8 @@
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
+ <Reference Include="System.Xaml" />
+ <Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Profile\EditProfile\EditProfileViewModelTests.cs" />
View
66 Tests/CDTag.ViewModels.Tests/Profile/NewProfile/NewProfileViewModelTests.cs
@@ -4,9 +4,11 @@
using CDTag.Common;
using CDTag.Common.ApplicationServices;
using CDTag.Common.Dispatcher;
+using CDTag.Common.Events;
using CDTag.Model.Profile;
using CDTag.Model.Profile.NewProfile;
using CDTag.ViewModels.Profile.NewProfile;
+using IdSharp.Common.Events;
using NUnit.Framework;
namespace CDTag.ViewModel.Tests.Profile.NewProfile
@@ -720,7 +722,8 @@ public void FinishFileExistsTest()
newProfileViewModel.Profile.ProfileName = _profileName;
bool closeWindowCalled = false;
- newProfileViewModel.CloseWindow = () => { closeWindowCalled = true; };
+ bool? closeWindowResult = null;
+ newProfileViewModel.CloseWindow = (res) => { closeWindowCalled = true; closeWindowResult = res; };
// Assert initial state
Assert.That(File.Exists(_unitTestsProfile), Is.False, string.Format("File.Exists({0})", _unitTestsProfile));
@@ -732,6 +735,7 @@ public void FinishFileExistsTest()
// Assert
Assert.That(File.Exists(_unitTestsProfile), Is.True, string.Format("File.Exists({0})", _unitTestsProfile));
Assert.That(closeWindowCalled, Is.True, "closeWindowCalled");
+ Assert.That(closeWindowResult, Is.True, "closeWindowResult");
}
[Test]
@@ -778,7 +782,8 @@ private void FinishTestParameters(FormatItem directoryFormat, FormatItem audioFo
newProfileViewModel.CreateNFO = createNFO;
bool closeWindowCalled = false;
- newProfileViewModel.CloseWindow = () => { closeWindowCalled = true; };
+ bool? closeWindowResult = null;
+ newProfileViewModel.CloseWindow = (res) => { closeWindowCalled = true; closeWindowResult = res; };
// Assert initial state
Assert.That(File.Exists(_unitTestsProfile), Is.False, string.Format("File.Exists({0})", _unitTestsProfile));
@@ -790,6 +795,7 @@ private void FinishTestParameters(FormatItem directoryFormat, FormatItem audioFo
// Assert
Assert.That(File.Exists(_unitTestsProfile), Is.True, string.Format("File.Exists({0})", _unitTestsProfile));
Assert.That(closeWindowCalled, Is.True, "closeWindowCalled");
+ Assert.That(closeWindowResult, Is.True, "closeWindowResult");
if (createNFO)
{
@@ -880,7 +886,8 @@ public void SampleNFOExistsNoOverwriteTest()
bool messageBoxShown = false;
newProfileViewModel.ShowMessageBox += (s, e) => { e.Data.Result = MessageBoxResult.No; messageBoxShown = true; }; // do not overwrite
bool closeWindowCalled = false;
- newProfileViewModel.CloseWindow = () => { closeWindowCalled = true; };
+ bool? closeWindowResult = null;
+ newProfileViewModel.CloseWindow = (res) => { closeWindowCalled = true; closeWindowResult = res; };
const string contents = "unittestfilecontents";
File.WriteAllText(_unitTestsNFO, contents); // create file
@@ -891,6 +898,7 @@ public void SampleNFOExistsNoOverwriteTest()
// Assert
Assert.That(messageBoxShown, Is.True, "messageBoxShown");
Assert.That(closeWindowCalled, Is.False, "closeWindowCalled");
+ Assert.That(closeWindowResult, Is.Null, "closeWindowResult");
Assert.That(File.Exists(_unitTestsNFO), Is.True, string.Format("File.Exists(\"{0}\")", _unitTestsNFO));
string actualContents = File.ReadAllText(_unitTestsNFO);
Assert.That(actualContents, Is.EqualTo(contents), "actualContents");
@@ -910,7 +918,8 @@ public void SampleNFOExistsOverwriteTest()
bool messageBoxShown = false;
newProfileViewModel.ShowMessageBox += (s, e) => { e.Data.Result = MessageBoxResult.Yes; messageBoxShown = true; }; // overwrite
bool closeWindowCalled = false;
- newProfileViewModel.CloseWindow = () => { closeWindowCalled = true; };
+ bool? closeWindowResult = null;
+ newProfileViewModel.CloseWindow = (res) => { closeWindowCalled = true; closeWindowResult = res; };
const string contents = "unittestfilecontents";
File.WriteAllText(_unitTestsNFO, contents); // create file
@@ -921,6 +930,7 @@ public void SampleNFOExistsOverwriteTest()
// Assert
Assert.That(messageBoxShown, Is.True, "messageBoxShown");
Assert.That(closeWindowCalled, Is.True, "closeWindowCalled");
+ Assert.That(closeWindowResult, Is.True, "closeWindowResult");
Assert.That(File.Exists(_unitTestsNFO), Is.True, string.Format("File.Exists(\"{0}\")", _unitTestsNFO));
string actualContents = File.ReadAllText(_unitTestsNFO);
Assert.That(actualContents, Is.EqualTo(UserProfile.GetSampleNFO()), "actualContents");
@@ -941,7 +951,8 @@ public void SampleNFODoesNotExistTest()
bool messageBoxShown = false;
newProfileViewModel.ShowMessageBox += (s, e) => { e.Data.Result = MessageBoxResult.No; messageBoxShown = true; }; // do not overwrite
bool closeWindowCalled = false;
- newProfileViewModel.CloseWindow = () => { closeWindowCalled = true; };
+ bool? closeWindowResult = null;
+ newProfileViewModel.CloseWindow = (res) => { closeWindowCalled = true; closeWindowResult = res; };
// Act
newProfileViewModel.NextCommand.Execute(null); // Page 1 -> Page 2
@@ -950,6 +961,7 @@ public void SampleNFODoesNotExistTest()
// Assert
Assert.That(messageBoxShown, Is.False, "messageBoxShown");
Assert.That(closeWindowCalled, Is.True, "closeWindowCalled");
+ Assert.That(closeWindowResult, Is.True, "closeWindowResult");
Assert.That(File.Exists(_unitTestsNFO), Is.True, string.Format("File.Exists(\"{0}\")", _unitTestsNFO));
string actualContents = File.ReadAllText(_unitTestsNFO);
Assert.That(actualContents, Is.EqualTo(UserProfile.GetSampleNFO()), "actualContents");
@@ -968,10 +980,14 @@ public void UseExistingNFOOpenDialogTrueTest()
newProfileViewModel.CreateNFO = true;
newProfileViewModel.HasExistingNFO = true;
bool closeWindowCalled = false;
- newProfileViewModel.CloseWindow = () => { closeWindowCalled = true; };
- UnitTestDialogService dialogService = (UnitTestDialogService)IoC.Resolve<IDialogService>();
- dialogService.ShowOpenFileDialogFileName = _unitTestsNFO;
- dialogService.ShowOpenFileDialogResult = true; // "Open"
+ bool? closeWindowResult = null;
+ newProfileViewModel.CloseWindow = (res) => { closeWindowCalled = true; closeWindowResult = res; };
+
+ UnitTestDialogService mockDialogService = (UnitTestDialogService)IoC.Resolve<IDialogService>();
+ mockDialogService.ShowOpenFileDialogFileName = _unitTestsNFO;
+ mockDialogService.ShowOpenFileDialogResult = true; // "Open"
+ newProfileViewModel.ShowOpenFile += (s, e) => ShowOpenFile(e, mockDialogService);
+
const string contents = "unittestfilecontents";
File.WriteAllText(_unitTestsNFO, contents); // create file
@@ -981,12 +997,20 @@ public void UseExistingNFOOpenDialogTrueTest()
// Assert
Assert.That(closeWindowCalled, Is.True, "closeWindowCalled");
+ Assert.That(closeWindowResult, Is.True, "closeWindowResult");
Assert.That(File.Exists(_unitTestsNFO), Is.True, string.Format("File.Exists(\"{0}\")", _unitTestsNFO));
string actualContents = File.ReadAllText(_unitTestsNFO);
Assert.That(actualContents, Is.EqualTo(contents), "actualContents");
Assert.That(newProfileViewModel.Profile.NFOOptions.TemplatePath, Is.EqualTo(_unitTestsNFO), "newProfileViewModel.Profile.NFOOptions.TemplatePath");
}
+ private void ShowOpenFile(DataEventArgs<ShowOpenFileDialogEvent> e, IDialogService mockDialogService)
+ {
+ string fileName;
+ e.Data.Result = mockDialogService.ShowOpenFileDialog(e.Data.Title, e.Data.FileName, null, out fileName);
+ e.Data.FileName = fileName;
+ }
+
[Test]
public void UseExistingNFOOpenDialogFalseTest()
{
@@ -999,10 +1023,13 @@ public void UseExistingNFOOpenDialogFalseTest()
newProfileViewModel.CreateNFO = true;
newProfileViewModel.HasExistingNFO = true;
bool closeWindowCalled = false;
- newProfileViewModel.CloseWindow = () => { closeWindowCalled = true; };
- UnitTestDialogService dialogService = (UnitTestDialogService)IoC.Resolve<IDialogService>();
- dialogService.ShowOpenFileDialogFileName = _unitTestsNFO;
- dialogService.ShowOpenFileDialogResult = false; // "Cancel"
+ bool? closeWindowResult = null;
+ newProfileViewModel.CloseWindow = (res) => { closeWindowCalled = true; closeWindowResult = res; };
+
+ UnitTestDialogService mockDialogService = (UnitTestDialogService)IoC.Resolve<IDialogService>();
+ mockDialogService.ShowOpenFileDialogFileName = _unitTestsNFO;
+ mockDialogService.ShowOpenFileDialogResult = false; // "Cancel"
+ newProfileViewModel.ShowOpenFile += (s, e) => ShowOpenFile(e, mockDialogService);
// Act
newProfileViewModel.NextCommand.Execute(null); // Page 1 -> Page 2
@@ -1010,6 +1037,7 @@ public void UseExistingNFOOpenDialogFalseTest()
// Assert
Assert.That(closeWindowCalled, Is.False, "closeWindowCalled");
+ Assert.That(closeWindowResult, Is.Null, "closeWindowResult");
Assert.That(File.Exists(_unitTestsNFO), Is.False, string.Format("File.Exists(\"{0}\")", _unitTestsNFO));
}
@@ -1025,10 +1053,13 @@ public void UseExistingNFOOpenDialogNullTest()
newProfileViewModel.CreateNFO = true;
newProfileViewModel.HasExistingNFO = true;
bool closeWindowCalled = false;
- newProfileViewModel.CloseWindow = () => { closeWindowCalled = true; };
- UnitTestDialogService dialogService = (UnitTestDialogService)IoC.Resolve<IDialogService>();
- dialogService.ShowOpenFileDialogFileName = _unitTestsNFO;
- dialogService.ShowOpenFileDialogResult = null; // "Close"
+ bool? closeWindowResult = null;
+ newProfileViewModel.CloseWindow = (res) => { closeWindowCalled = true; closeWindowResult = res; };
+
+ UnitTestDialogService mockDialogService = (UnitTestDialogService)IoC.Resolve<IDialogService>();
+ mockDialogService.ShowOpenFileDialogFileName = _unitTestsNFO;
+ mockDialogService.ShowOpenFileDialogResult = null; // "Close"
+ newProfileViewModel.ShowOpenFile += (s, e) => ShowOpenFile(e, mockDialogService);
// Act
newProfileViewModel.NextCommand.Execute(null); // Page 1 -> Page 2
@@ -1036,6 +1067,7 @@ public void UseExistingNFOOpenDialogNullTest()
// Assert
Assert.That(closeWindowCalled, Is.False, "closeWindowCalled");
+ Assert.That(closeWindowResult, Is.Null, "closeWindowResult");
Assert.That(File.Exists(_unitTestsNFO), Is.False, string.Format("File.Exists(\"{0}\")", _unitTestsNFO));
}
}
View
20 Tests/CDTag.ViewModels.Tests/UnitTestDialogService.cs
@@ -1,4 +1,5 @@
using System;
+using System.Windows;
using CDTag.Common.ApplicationServices;
using CDTag.Common.Wpf;
using CDTag.Views.Interfaces;
@@ -28,13 +29,24 @@ public void CloseAddressTextBox()
throw new NotImplementedException();
}
- public bool? ShowOpenFileDialog(string title, string filter, out string fileName)
+ public bool? ShowOpenFileDialogResult { get; set; }
+ public string ShowOpenFileDialogFileName { get; set; }
+
+ public bool? ShowWindow(IWindow window, Window owner)
+ {
+ throw new NotImplementedException();
+ }
+
+ public bool? ShowWindow<T>(Window owner)
+ where T : IWindow
+ {
+ throw new NotImplementedException();
+ }
+
+ public bool? ShowOpenFileDialog(string title, string filter, Window owner, out string fileName)
{
fileName = ShowOpenFileDialogFileName;
return ShowOpenFileDialogResult;
}
-
- public bool? ShowOpenFileDialogResult { get; set; }
- public string ShowOpenFileDialogFileName { get; set; }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.