Implement passing test for ElementPickerViewModel.

This commit is contained in:
Daniel Kraus 2015-08-03 14:31:35 +02:00
parent 8437f0907c
commit 805da69d47
3 changed files with 135 additions and 7 deletions

View File

@ -91,6 +91,7 @@
<Compile Include="Thesaurus\TestThesaurus.cs" /> <Compile Include="Thesaurus\TestThesaurus.cs" />
<Compile Include="Importer\ZaaImporter\TimePointTest.cs" /> <Compile Include="Importer\ZaaImporter\TimePointTest.cs" />
<Compile Include="TestHelpers.cs" /> <Compile Include="TestHelpers.cs" />
<Compile Include="ViewModels\ElementPickerViewModelTest.cs" />
<Compile Include="ViewModels\SettingsRepositoryViewModelTest.cs" /> <Compile Include="ViewModels\SettingsRepositoryViewModelTest.cs" />
<Compile Include="ViewModels\SettingsViewModelTest.cs" /> <Compile Include="ViewModels\SettingsViewModelTest.cs" />
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,54 @@
/* ElementPickerViewModelTest.cs
* part of zaaReloaded2
*
* Copyright 2015 Daniel Kraus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Bovender.Mvvm.ViewModels;
using zaaReloaded2.ViewModels;
namespace Tests.ViewModels
{
[TestFixture]
class ElementPickerViewModelTest
{
[Test]
public void ChooseElement()
{
ElementPickerViewModel vm = new ElementPickerViewModel(true);
ViewModelBase elementVM = vm.Categories.First().Children.First();
elementVM.IsSelected = true;
string expectedLabel = elementVM.DisplayString;
Assert.IsTrue(vm.ChooseElementCommand.CanExecute(null),
"Cannot execute ChooseElementCommand");
bool messageSent = false;
vm.ElementChosenMessage.Sent += (sender, args) =>
{
messageSent = true;
ViewModelBase messageContentVM = args.Content.ViewModel as ViewModelBase;
Assert.AreEqual(expectedLabel, messageContentVM.DisplayString);
};
vm.ChooseElementCommand.Execute(null);
Assert.IsTrue(messageSent, "No message was sent when element was chosen");
}
}
}

View File

@ -38,19 +38,65 @@ namespace zaaReloaded2.ViewModels
/// <summary> /// <summary>
/// A two-dimensional tree /// A two-dimensional tree
/// </summary> /// </summary>
public IList<CategoryViewModel> AvailableElements { get; private set; } public IList<CategoryViewModel> Categories { get; private set; }
/// <summary>
/// Gets the currently selected element, or null if no element is selected.
/// </summary>
/// <remarks>
/// TODO: Raise PropertyChanged event.
/// </remarks>
public ElementViewModel Selected { get; private set; }
#endregion
#region Commands
public DelegatingCommand ChooseElementCommand
{
get
{
if (_chooseElementCommand == null)
{
_chooseElementCommand = new DelegatingCommand(
param => DoChooseElement(),
param => CanChooseElement());
}
return _chooseElementCommand;
}
}
#endregion
#region Messages
/// <summary>
/// A message that is sent out when an element has been chosen;
/// the message content is the element's view model with the element
/// wrapped inside.
/// </summary>
public Message<ViewModelMessageContent> ElementChosenMessage
{
get
{
if (_elementChosenMessage == null)
{
_elementChosenMessage = new Message<ViewModelMessageContent>();
}
return _elementChosenMessage;
}
}
#endregion #endregion
#region Constructors #region Constructors
public ElementPickerViewModel(SettingsViewModel caller, bool allowControlElements) public ElementPickerViewModel(bool allowControlElements)
{ {
_caller = caller; Categories = new List<CategoryViewModel>();
AvailableElements = new List<CategoryViewModel>();
if (allowControlElements) if (allowControlElements)
{ {
AvailableElements.Add( Categories.Add(
new CategoryViewModel( new CategoryViewModel(
"Kontroll-Elemente", "Kontroll-Elemente",
new Collection<ViewModelBase>() new Collection<ViewModelBase>()
@ -62,7 +108,7 @@ namespace zaaReloaded2.ViewModels
) )
); );
} }
AvailableElements.Add( Categories.Add(
new CategoryViewModel( new CategoryViewModel(
"Ausgabe-Elemente", "Ausgabe-Elemente",
new Collection<ViewModelBase>() new Collection<ViewModelBase>()
@ -87,6 +133,7 @@ namespace zaaReloaded2.ViewModels
{ {
ControlElementViewModel vm = new ControlElementViewModel(element); ControlElementViewModel vm = new ControlElementViewModel(element);
vm.DisplayString = element.Label; vm.DisplayString = element.Label;
vm.PropertyChanged += ElementViewModel_PropertyChanged;
return vm; return vm;
} }
@ -102,6 +149,31 @@ namespace zaaReloaded2.ViewModels
return vm; return vm;
} }
void ElementViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "IsSelected")
{
// Casting sender as ElementViewModel may result in Null, but this
// is a desired effect because category headings may be selected
// in a view, but are not valid ElementViewModels that we could
// 'choose'.
Selected = sender as ElementViewModel;
}
}
void DoChooseElement()
{
if (CanChooseElement())
{
ElementChosenMessage.Send(new ViewModelMessageContent(Selected));
}
}
bool CanChooseElement()
{
return Selected != null;
}
#endregion #endregion
#region Implementation ov ViewModelBase #region Implementation ov ViewModelBase
@ -115,7 +187,8 @@ namespace zaaReloaded2.ViewModels
#region Fields #region Fields
SettingsViewModel _caller; DelegatingCommand _chooseElementCommand;
Message<ViewModelMessageContent> _elementChosenMessage;
#endregion #endregion
} }