Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored dialogs

-Dialog service now returns a view model as result
-DjalogFactory has simpler interface
-Added xUnit extensions to use test-cases
-Made AdminTool.Tests non CLS-compliant
-Fixed build script to generate AssemblyInfo for Tests as non CLS-compliant
  • Loading branch information...
commit f279d45e5ae3a0e0fba155c226f98e020e83c2c9 1 parent 6b57193
@HEskandari HEskandari authored
View
45 Rhino.Licensing.AdminTool.Tests/Dialogs/FileDialogTests.cs
@@ -3,23 +3,30 @@
using Rhino.Mocks;
using Xunit;
using System.Linq;
+using Xunit.Extensions;
using FileDialog = Rhino.Licensing.AdminTool.Dialogs.FileDialog;
+using DialogForm = System.Windows.Forms.FileDialog;
namespace Rhino.Licensing.AdminTool.Tests.Dialogs
{
public class FileDialogTests
{
+ private DialogForm _dialogForm;
+
+ public FileDialogTests()
+ {
+ _dialogForm = MockRepository.GenerateMock<DialogForm>();
+ }
+
[Fact]
public void ShowDialog_Sets_Selected_Files()
{
- var dialogForm = MockRepository.GenerateMock<System.Windows.Forms.FileDialog>();
-
var model = new OpenFileDialogViewModel();
- var dialog = new TestFileDialog(dialogForm, model);
+ var dialog = new TestFileDialog(_dialogForm, model);
- dialogForm.Expect(x => x.ShowDialog()).Return(DialogResult.OK);
- dialogForm.Expect(x => x.FileName).Return("License.lic");
- dialogForm.Expect(x => x.FileNames).Return(new[] {"License.lic", "License2.lic"});
+ _dialogForm.Expect(x => x.ShowDialog()).Return(DialogResult.OK);
+ _dialogForm.Expect(x => x.FileName).Return("License.lic");
+ _dialogForm.Expect(x => x.FileNames).Return(new[] {"License.lic", "License2.lic"});
dialog.ShowDialog();
@@ -32,21 +39,35 @@ public void ShowDialog_Sets_Selected_Files()
[Fact]
public void Dialog_Disposes_Upon_Destruction()
{
- var dialogForm = MockRepository.GenerateMock<System.Windows.Forms.FileDialog>();
-
var model = new OpenFileDialogViewModel();
- var dialog = new TestFileDialog(dialogForm, model);
+ var dialog = new TestFileDialog(_dialogForm, model);
dialog.ShowDialog();
dialog.Dispose();
- dialogForm.AssertWasCalled(x => x.Dispose(), x => x.Repeat.Once());
+ _dialogForm.AssertWasCalled(x => x.Dispose(), x => x.Repeat.Once());
+ }
+
+ [Theory]
+ [InlineData(DialogResult.OK, true)]
+ [InlineData(DialogResult.Cancel, false)]
+ [InlineData(DialogResult.Abort, null)]
+ public void DialogResult_Maps_To_ViewModel_Result(DialogResult dialogResult, bool? mappedResult)
+ {
+ var model = new OpenFileDialogViewModel();
+ var dialog = new TestFileDialog(_dialogForm, model);
+
+ _dialogForm.Expect(x => x.ShowDialog()).Return(dialogResult);
+
+ dialog.ShowDialog();
+
+ Assert.Equal(mappedResult, model.Result);
}
public class TestFileDialog : FileDialog
{
- private System.Windows.Forms.FileDialog _dialog;
- private IFileDialogViewModel _viewModel;
+ private readonly System.Windows.Forms.FileDialog _dialog;
+ private readonly IFileDialogViewModel _viewModel;
public TestFileDialog(System.Windows.Forms.FileDialog dialog, IFileDialogViewModel viewModel)
{
View
3  Rhino.Licensing.AdminTool.Tests/Rhino.Licensing.AdminTool.Tests.csproj
@@ -81,6 +81,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\SharedLibs\xunit.dll</HintPath>
</Reference>
+ <Reference Include="xunit.extensions">
+ <HintPath>..\SharedLibs\xunit.extensions.dll</HintPath>
+ </Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Dialogs\FileDialogTests.cs" />
View
74 Rhino.Licensing.AdminTool.Tests/Services/DialogServiceTests.cs
@@ -1,4 +1,3 @@
-using System.Windows.Forms;
using Rhino.Licensing.AdminTool.Factories;
using Rhino.Licensing.AdminTool.Services;
using Rhino.Licensing.AdminTool.ViewModels;
@@ -14,15 +13,14 @@ public class DialogServiceTests
[Fact]
public void Can_Show_OpenFileDialog()
{
- var model = CreateOpenFileDialogModel();
+ var model = CreateOpenFileDialogModel(true);
var factory = MockRepository.GenerateMock<IDialogFactory>();
var dialog = MockRepository.GenerateMock<OpenFileDialog>(model);
- factory.Expect(x => x.Create<OpenFileDialog>(Arg.Is(model))).Return(dialog);
+ factory.Expect(x => x.Create<OpenFileDialog>()).Return(dialog);
dialog.Expect(x => x.ViewModel).Return(model);
- dialog.Expect(x => x.ShowDialog()).Return(DialogResult.OK);
- new DialogService(factory).ShowOpenFileDialog(model);
+ new DialogService(factory).ShowOpenFileDialog();
dialog.AssertWasCalled(x => x.ShowDialog(), x => x.Repeat.Once());
}
@@ -30,70 +28,20 @@ public void Can_Show_OpenFileDialog()
[Fact]
public void Can_Show_SaveFileDialog()
{
- var model = CreateSaveFileDialogModel();
+ var model = CreateSaveFileDialogModel(true);
var factory = MockRepository.GenerateMock<IDialogFactory>();
var dialog = MockRepository.GenerateMock<SaveFileDialog>(model);
var service = new DialogService(factory) as IDialogService;
- factory.Expect(x => x.Create<SaveFileDialog>(Arg.Is(model))).Return(dialog);
+ factory.Expect(x => x.Create<SaveFileDialog>()).Return(dialog);
- service.ShowSaveFileDialog(model);
+ service.ShowSaveFileDialog();
dialog.AssertWasCalled(x => x.ShowDialog(), x => x.Repeat.Once());
}
- [Fact]
- public void Returning_OK_As_DialogResult_Translates_To_True()
- {
- var model = CreateOpenFileDialogModel();
- var factory = MockRepository.GenerateMock<IDialogFactory>();
- var dialog = MockRepository.GenerateMock<OpenFileDialog>(model);
-
- factory.Expect(x => x.Create<OpenFileDialog>(Arg.Is(model))).Return(dialog);
- dialog.Expect(x => x.ViewModel).Return(model);
- dialog.Expect(x => x.ShowDialog()).Return(DialogResult.OK);
-
- var result = new DialogService(factory).ShowOpenFileDialog(model);
-
- Assert.NotNull(result);
- Assert.True(result.Value);
- }
-
- [Fact]
- public void Returning_Cancel_As_DialogResult_Translates_To_False()
- {
- var model = CreateOpenFileDialogModel();
- var factory = MockRepository.GenerateMock<IDialogFactory>();
- var dialog = MockRepository.GenerateMock<OpenFileDialog>(model);
-
- factory.Expect(x => x.Create<OpenFileDialog>(Arg.Is(model))).Return(dialog);
- dialog.Expect(x => x.ViewModel).Return(model);
- dialog.Expect(x => x.ShowDialog()).Return(DialogResult.Cancel);
-
- var result = new DialogService(factory).ShowOpenFileDialog(model);
-
- Assert.NotNull(result);
- Assert.False(result.Value);
- }
-
- [Fact]
- public void Returning_Anything_Else_From_DialogResult_Translates_To_Null()
- {
- var model = CreateOpenFileDialogModel();
- var factory = MockRepository.GenerateMock<IDialogFactory>();
- var dialog = MockRepository.GenerateMock<OpenFileDialog>(model);
-
- factory.Expect(x => x.Create<OpenFileDialog>(Arg.Is(model))).Return(dialog);
- dialog.Expect(x => x.ViewModel).Return(model);
- dialog.Expect(x => x.ShowDialog()).Return(DialogResult.Abort);
-
- var result = new DialogService(factory).ShowOpenFileDialog(model);
-
- Assert.Null(result);
- }
-
- private IOpenFileDialogViewModel CreateOpenFileDialogModel()
+ private IOpenFileDialogViewModel CreateOpenFileDialogModel(bool? result)
{
return new OpenFileDialogViewModel
{
@@ -104,11 +52,12 @@ private IOpenFileDialogViewModel CreateOpenFileDialogModel()
Filter = "Rhino License Project|*.rlic",
InitialDirectory = "C:\\",
MultiSelect = false,
- Title = "Open File Dialog"
+ Title = "Open File Dialog",
+ Result = result
};
}
- private ISaveFileDialogViewModel CreateSaveFileDialogModel()
+ private ISaveFileDialogViewModel CreateSaveFileDialogModel(bool? result)
{
return new SaveFileDialogViewModel
{
@@ -118,7 +67,8 @@ private ISaveFileDialogViewModel CreateSaveFileDialogModel()
DefaultExtension = "rlic",
Filter = "Rhino License Project|*.rlic",
InitialDirectory = "C:\\",
- Title = "Open File Dialog"
+ Title = "Open File Dialog",
+ Result = result
};
}
View
44 Rhino.Licensing.AdminTool.Tests/ViewModels/ProjectViewModelTests.cs
@@ -83,32 +83,52 @@ public void Can_Save_If_Name_Is_Provided()
[Fact]
public void Save_Action_Will_Open_SaveDialog()
{
+ _dialogService.Expect(x => x.ShowSaveFileDialog())
+ .Return(new SaveFileDialogViewModel
+ {
+ Result = true
+ });
+
var vm = CreateViewModel();
+ vm.Save();
+
+ _dialogService.AssertWasCalled(x => x.ShowSaveFileDialog(), x => x.Repeat.Once());
+ }
+ [Fact]
+ public void Save_Action_Will_Call_ProjectService_If_Proper_Result_Is_Set()
+ {
+ var existingFile = Path.GetTempFileName();
+ var choosenFile = new FileInfo(existingFile);
+
+ _dialogService.Expect(x => x.ShowSaveFileDialog())
+ .Return(new SaveFileDialogViewModel
+ {
+ Result = true,
+ FileName = existingFile
+ });
+
+ var vm = CreateViewModel();
vm.Save();
- _dialogService.AssertWasCalled(x => x.ShowSaveFileDialog(Arg<ISaveFileDialogViewModel>.Is.Anything), x => x.Repeat.Once());
+ _projectService.Expect(x => x.Save(Arg<Project>.Is.Anything, Arg.Is(choosenFile))).Repeat.Once();
}
[Fact]
- public void Will_Proceed_To_Save_When_Dialog_Successfully_Closes()
+ public void Will_Not_Proceed_To_Save_When_No_File_Is_Selected()
{
var vm = CreateViewModel();
- var project = new Project();
- var temp = Path.GetTempFileName();
- _dialogService.Expect(x => x.ShowSaveFileDialog(Arg<ISaveFileDialogViewModel>.Is.Anything))
- .WhenCalled(m =>
+ _dialogService.Expect(x => x.ShowSaveFileDialog())
+ .Return(new SaveFileDialogViewModel
{
- var model = (SaveFileDialogViewModel) m.Arguments[0];
- model.FileName = temp;
- })
- .Return(true);
+ Result = true,
+ FileName = null
+ });
- vm.CurrentProject = project;
vm.Save();
- _projectService.AssertWasCalled(x => x.Save(Arg.Is(project), Arg<FileInfo>.Is.Anything));
+ _projectService.AssertWasNotCalled(x => x.Save(Arg<Project>.Is.Anything, Arg<FileInfo>.Is.Anything));
}
[Fact]
View
22 Rhino.Licensing.AdminTool/Dialogs/FileDialog.cs
@@ -6,14 +6,13 @@ namespace Rhino.Licensing.AdminTool.Dialogs
{
public abstract class FileDialog : IDisposable
{
- public virtual DialogResult ShowDialog()
+ public virtual void ShowDialog()
{
var result = Dialog.ShowDialog();
ViewModel.FileName = Dialog.FileName;
- ViewModel.FileNames = Dialog.FileNames;
-
- return result;
+ ViewModel.FileNames = Dialog.FileNames;
+ ViewModel.Result = MappedResult(result);
}
protected abstract System.Windows.Forms.FileDialog Dialog
@@ -46,6 +45,21 @@ protected virtual void Dispose(bool disposing)
Dialog.Dispose();
}
}
+ }
+
+ private static bool? MappedResult(DialogResult result)
+ {
+ if (result == DialogResult.OK)
+ {
+ return true;
+ }
+
+ if (result == DialogResult.Cancel)
+ {
+ return false;
+ }
+
+ return null;
}
}
}
View
3  Rhino.Licensing.AdminTool/Factories/IDialogFactory.cs
@@ -8,10 +8,9 @@ public interface IDialogFactory
/// <summary>
/// Creates a new FileDialog
/// </summary>
- /// <param name="dialogModel"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
- T Create<T>(IFileDialogViewModel dialogModel) where T : FileDialog;
+ T Create<T>() where T : FileDialog;
/// <summary>
/// Releases file dialog for garbage collection
View
6 Rhino.Licensing.AdminTool/Rhino.Licensing.AdminTool.csproj
@@ -108,6 +108,7 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
+ <Compile Include="Controls\FXPopup.cs" />
<Compile Include="Dialogs\FileDialog.cs" />
<Compile Include="Dialogs\OpenFileDialog.cs" />
<Compile Include="Dialogs\SaveFileDialog.cs" />
@@ -125,6 +126,7 @@
<Compile Include="Startup\ServiceRegistration.cs" />
<Compile Include="Startup\ViewModelRegistration.cs" />
<Compile Include="Startup\ViewRegistration.cs" />
+ <Compile Include="ValueConverters\BoolToVisibilityCollapsedConverter.cs" />
<Compile Include="ViewModels\AboutViewModel.cs" />
<Compile Include="ViewModels\FileDialogViewModel.cs" />
<Compile Include="ViewModels\OpenFileDialogViewModel.cs" />
@@ -148,6 +150,10 @@
<AppDesigner Include="Properties\" />
</ItemGroup>
<ItemGroup>
+ <Page Include="Theme\ExpressionDark.xaml">
+ <SubType>Designer</SubType>
+ <Generator>MSBuild:Compile</Generator>
+ </Page>
<Page Include="Views\AboutView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
View
41 Rhino.Licensing.AdminTool/Services/DialogService.cs
@@ -1,4 +1,3 @@
-using System.Windows.Forms;
using Rhino.Licensing.AdminTool.Factories;
using Rhino.Licensing.AdminTool.ViewModels;
using OpenFileDialog = Rhino.Licensing.AdminTool.Dialogs.OpenFileDialog;
@@ -11,16 +10,14 @@ public interface IDialogService
/// <summary>
/// Shows a OpenFileDialog
/// </summary>
- /// <param name="dialogModel"></param>
/// <returns></returns>
- bool? ShowOpenFileDialog(IOpenFileDialogViewModel dialogModel);
+ IOpenFileDialogViewModel ShowOpenFileDialog();
/// <summary>
/// Shows a SaveFileDialog
/// </summary>
- /// <param name="dialogModel"></param>
/// <returns></returns>
- bool? ShowSaveFileDialog(ISaveFileDialogViewModel dialogModel);
+ ISaveFileDialogViewModel ShowSaveFileDialog();
}
public class DialogService : IDialogService
@@ -32,39 +29,27 @@ public DialogService(IDialogFactory dialogFactory)
_dialogFactory = dialogFactory;
}
- public bool? ShowOpenFileDialog(IOpenFileDialogViewModel dialogModel)
+ public virtual IOpenFileDialogViewModel ShowOpenFileDialog()
{
- var dialog = _dialogFactory.Create<OpenFileDialog>(dialogModel);
- var result = dialog.ShowDialog();
+ var dialog = _dialogFactory.Create<OpenFileDialog>();
+
+ dialog.ShowDialog();
_dialogFactory.Release(dialog);
- return MappedResult(result);
+ return dialog.ViewModel as IOpenFileDialogViewModel;
}
- public bool? ShowSaveFileDialog(ISaveFileDialogViewModel dialogModel)
+ public virtual ISaveFileDialogViewModel ShowSaveFileDialog()
{
- var dialog = _dialogFactory.Create<SaveFileDialog>(dialogModel);
- var result = dialog.ShowDialog();
-
- _dialogFactory.Release(dialog);
+ var dialog = _dialogFactory.Create<SaveFileDialog>();
- return MappedResult(result);
- }
+ dialog.ShowDialog();
- private static bool? MappedResult(DialogResult result)
- {
- if (result == DialogResult.OK)
- {
- return true;
- }
-
- if (result == DialogResult.Cancel)
- {
- return false;
- }
+ _dialogFactory.Release(dialog);
- return null;
+ return dialog.ViewModel as ISaveFileDialogViewModel;
}
+
}
}
View
17 Rhino.Licensing.AdminTool/Startup/ViewModelRegistration.cs
@@ -1,8 +1,10 @@
+using System.Collections.Generic;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
using Rhino.Licensing.AdminTool.Extensions;
-using Rhino.Licensing.AdminTool.ViewModels;
-
+using Rhino.Licensing.AdminTool.ViewModels;
+using System.Linq;
+
namespace Rhino.Licensing.AdminTool.Startup
{
public class ViewModelRegistration : IRegistration
@@ -10,10 +12,19 @@ public class ViewModelRegistration : IRegistration
public virtual void Register(IKernel kernel)
{
kernel.Register(AllTypes.FromAssemblyContaining<ViewModelRegistration>()
- .Where(t => t.Namespace == "Rhino.Licensing.AdminTool.ViewModels")
+ .Where(t => ViewModelNamespaces.Contains(t.Namespace))
.WithService.FirstInterfaceOnClass()
.Configure(c => c.LifeStyle.Transient)
.ConfigureFor<IShellViewModel>(c => c.LifeStyle.Singleton));
}
+
+ private static IEnumerable<string> ViewModelNamespaces
+ {
+ get
+ {
+ yield return "Rhino.Licensing.AdminTool.ViewModels";
+ yield return "Rhino.Licensing.AdminTool.Dialogs";
+ }
+ }
}
}
View
12 Rhino.Licensing.AdminTool/ViewModels/FileDialogViewModel.cs
@@ -15,6 +15,7 @@ public interface IFileDialogViewModel
string Title { get; set; }
string DisplayName { get; set; }
IEnumerable<string> FileNames { get; set; }
+ bool? Result { get; set; }
}
public abstract class FileDialogViewModel : Screen, IFileDialogViewModel
@@ -28,6 +29,7 @@ public abstract class FileDialogViewModel : Screen, IFileDialogViewModel
private string _filter;
private string _initialDirectory;
private string _title;
+ private bool? _result;
public virtual bool AddExtension
{
@@ -89,6 +91,16 @@ public virtual IEnumerable<string> FileNames
}
}
+ public virtual bool? Result
+ {
+ get { return _result; }
+ set
+ {
+ _result = value;
+ NotifyOfPropertyChange(() => Result);
+ }
+ }
+
public virtual string Filter
{
get { return _filter; }
View
7 Rhino.Licensing.AdminTool/ViewModels/ProjectViewModel.cs
@@ -49,12 +49,11 @@ public virtual bool CanSave()
[AutoCheckAvailability]
public virtual void Save()
{
- var dialog = new SaveFileDialogViewModel();
- var result = _dialogService.ShowSaveFileDialog(dialog);
+ var dialogModel = _dialogService.ShowSaveFileDialog();
- if (result.GetValueOrDefault(false))
+ if (dialogModel.Result.GetValueOrDefault(false) && File.Exists(dialogModel.FileName))
{
- _projectService.Save(CurrentProject, new FileInfo(dialog.FileName));
+ _projectService.Save(CurrentProject, new FileInfo(dialogModel.FileName));
}
}
}
View
BIN  SharedLibs/xunit.extensions.dll
Binary file not shown
View
1  default.ps1
@@ -57,6 +57,7 @@ task Init -depends Clean {
-company "Hibernating Rhinos" `
-product "Rhino Licensing $version" `
-version $version `
+ -clsCompliant "false" `
-copyright "Hibernating Rhinos & Ayende Rahien 2004 - 2009"
new-item $release_dir -itemType directory
Please sign in to comment.
Something went wrong with that request. Please try again.