Implement tests for SettingsViewModel add and add child commands.

This commit is contained in:
Daniel Kraus 2015-08-04 01:17:14 +02:00
parent 805da69d47
commit a128b76522
3 changed files with 98 additions and 7 deletions

View File

@ -40,13 +40,52 @@ namespace Tests.ViewModels
[Test] [Test]
public void AddElement() public void AddElement()
{ {
bool messageSent = false;
Settings model = _settingsVM.RevealModelObject() as Settings;
int oldViewModelElementCount = _settingsVM.Elements.Count;
int oldModelElementCount = model.Elements.Count;
_settingsVM.AddElementMessage.Sent += (sender, args) =>
{
messageSent = true;
ElementPickerViewModel picker = args.Content.ViewModel as ElementPickerViewModel;
Assert.IsNotNull(picker, "ViewModel in MessageContent is not an ElementPickerViewModel");
picker.Categories.First().Children.First().IsSelected = true;
picker.ChooseElementCommand.Execute(null);
};
_settingsVM.AddElementCommand.Execute(null);
Assert.IsTrue(messageSent, "Message was not sent");
Assert.AreEqual(oldViewModelElementCount + 1, _settingsVM.Elements.Count,
"Count of elements in ViewModel was not increased by 1");
Assert.AreEqual(oldModelElementCount + 1, model.Elements.Count,
"Count of elements in settings Model was not increased by 1");
} }
[Test] [Test]
public void AddChildElement() public void AddChildElement()
{ {
bool messageSent = false;
SelectEachDay model = new SelectEachDay();
ControlElementViewModel viewModel = new ControlElementViewModel(model);
_settingsVM.AddElementViewModel(viewModel);
viewModel.IsSelected = true;
int oldViewModelChildrenCount = viewModel.Elements.Count;
int oldModelChildrenCount = model.FormatElements.Count;
_settingsVM.AddChildElementMessage.Sent += (sender, args) =>
{
messageSent = true;
ElementPickerViewModel picker = args.Content.ViewModel as ElementPickerViewModel;
Assert.IsNotNull(picker, "ViewModel in MessageContent is not an ElementPickerViewModel");
picker.Categories.First().Children.First().IsSelected = true;
Assert.IsTrue(picker.ChooseElementCommand.CanExecute(null),
"Cannot execute element picker's ChooseElementCommand.");
picker.ChooseElementCommand.Execute(null);
};
_settingsVM.AddChildElementCommand.Execute(null);
Assert.IsTrue(messageSent, "Message was not sent");
Assert.AreEqual(oldViewModelChildrenCount + 1, viewModel.Elements.Count,
"Count of children in ViewModel was not increased by 1");
Assert.AreEqual(oldModelChildrenCount + 1, model.FormatElements.Count,
"Count of children in Model was not increased by 1");
} }
[Test] [Test]
@ -67,13 +106,13 @@ namespace Tests.ViewModels
[Test] [Test]
public void DeleteElement() public void DeleteElement()
{ {
throw new NotImplementedException();
} }
[Test] [Test]
public void CopyElement() public void CopyElement()
{ {
throw new NotImplementedException();
} }
} }
} }

View File

@ -146,6 +146,7 @@ namespace zaaReloaded2.ViewModels
{ {
FormatElementViewModel vm = new FormatElementViewModel(element); FormatElementViewModel vm = new FormatElementViewModel(element);
vm.DisplayString = name; vm.DisplayString = name;
vm.PropertyChanged += ElementViewModel_PropertyChanged;
return vm; return vm;
} }

View File

@ -145,6 +145,30 @@ namespace zaaReloaded2.ViewModels
#region Messages #region Messages
public Message<ViewModelMessageContent> AddElementMessage
{
get
{
if (_addElementMessage == null)
{
_addElementMessage = new Message<ViewModelMessageContent>();
}
return _addElementMessage;
}
}
public Message<ViewModelMessageContent> AddChildElementMessage
{
get
{
if (_addChildElementMessage == null)
{
_addChildElementMessage = new Message<ViewModelMessageContent>();
}
return _addChildElementMessage;
}
}
#endregion #endregion
#region Commands #region Commands
@ -216,6 +240,7 @@ namespace zaaReloaded2.ViewModels
{ {
elementViewModel.PropertyChanged += ElementViewModel_PropertyChanged; elementViewModel.PropertyChanged += ElementViewModel_PropertyChanged;
Elements.Add(elementViewModel); Elements.Add(elementViewModel);
_settings.Elements.Add(elementViewModel.RevealModelObject() as ElementBase);
} }
/// <summary> /// <summary>
@ -233,9 +258,34 @@ namespace zaaReloaded2.ViewModels
#region Private methods #region Private methods
void DoAddElement() { } void DoAddElement()
{
// Create a new element picker; it will automatically create and
// send us a new element view model if one is chosen by the view.
ElementPickerViewModel picker = new ElementPickerViewModel(true);
picker.ElementChosenMessage.Sent += (sender, args) =>
{
ElementViewModel newVM = args.Content.ViewModel as ElementViewModel;
AddElementViewModel(newVM);
};
AddElementMessage.Send(new ViewModelMessageContent(picker));
}
void DoAddChildElement() { } void DoAddChildElement()
{
if (CanAddChildElement())
{
// Create a new element picker; it will automatically create and
// send us a new element view model if one is chosen by the view.
ElementPickerViewModel picker = new ElementPickerViewModel(false);
picker.ElementChosenMessage.Sent += (sender, args) =>
{
FormatElementViewModel newVM = args.Content.ViewModel as FormatElementViewModel;
AddChildElementViewModel(SelectedElement as ControlElementViewModel, newVM);
};
AddChildElementMessage.Send(new ViewModelMessageContent(picker));
}
}
bool CanAddChildElement() bool CanAddChildElement()
{ {
@ -290,7 +340,8 @@ namespace zaaReloaded2.ViewModels
DelegatingCommand _addChildElementCommand; DelegatingCommand _addChildElementCommand;
DelegatingCommand _deleteElementCommand; DelegatingCommand _deleteElementCommand;
DelegatingCommand _copyElementCommand; DelegatingCommand _copyElementCommand;
Message<ViewModelMessageContent> _addElementMessage;
Message<ViewModelMessageContent> _addChildElementMessage;
List<ElementViewModel> _elements; List<ElementViewModel> _elements;
ElementViewModel _selectedElement; ElementViewModel _selectedElement;
EnumProvider<ReferenceStyle> _referenceStyle; EnumProvider<ReferenceStyle> _referenceStyle;