From 805da69d476b09e099c683965a218a6d9d5b4323 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Mon, 3 Aug 2015 14:31:35 +0200 Subject: [PATCH] Implement passing test for ElementPickerViewModel. --- Tests/Tests.csproj | 1 + .../ViewModels/ElementPickerViewModelTest.cs | 54 ++++++++++++ .../ViewModels/ElementPickerViewModel.cs | 87 +++++++++++++++++-- 3 files changed, 135 insertions(+), 7 deletions(-) create mode 100755 Tests/ViewModels/ElementPickerViewModelTest.cs diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index fb63329..fc37c1f 100755 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -91,6 +91,7 @@ + diff --git a/Tests/ViewModels/ElementPickerViewModelTest.cs b/Tests/ViewModels/ElementPickerViewModelTest.cs new file mode 100755 index 0000000..6240d0b --- /dev/null +++ b/Tests/ViewModels/ElementPickerViewModelTest.cs @@ -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"); + } + } +} diff --git a/zaaReloaded2/ViewModels/ElementPickerViewModel.cs b/zaaReloaded2/ViewModels/ElementPickerViewModel.cs index 24d75eb..1279af6 100755 --- a/zaaReloaded2/ViewModels/ElementPickerViewModel.cs +++ b/zaaReloaded2/ViewModels/ElementPickerViewModel.cs @@ -38,19 +38,65 @@ namespace zaaReloaded2.ViewModels /// /// A two-dimensional tree /// - public IList AvailableElements { get; private set; } + public IList Categories { get; private set; } + + /// + /// Gets the currently selected element, or null if no element is selected. + /// + /// + /// TODO: Raise PropertyChanged event. + /// + 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 + + /// + /// 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. + /// + public Message ElementChosenMessage + { + get + { + if (_elementChosenMessage == null) + { + _elementChosenMessage = new Message(); + } + return _elementChosenMessage; + } + } #endregion #region Constructors - public ElementPickerViewModel(SettingsViewModel caller, bool allowControlElements) + public ElementPickerViewModel(bool allowControlElements) { - _caller = caller; - AvailableElements = new List(); + Categories = new List(); if (allowControlElements) { - AvailableElements.Add( + Categories.Add( new CategoryViewModel( "Kontroll-Elemente", new Collection() @@ -62,7 +108,7 @@ namespace zaaReloaded2.ViewModels ) ); } - AvailableElements.Add( + Categories.Add( new CategoryViewModel( "Ausgabe-Elemente", new Collection() @@ -87,6 +133,7 @@ namespace zaaReloaded2.ViewModels { ControlElementViewModel vm = new ControlElementViewModel(element); vm.DisplayString = element.Label; + vm.PropertyChanged += ElementViewModel_PropertyChanged; return vm; } @@ -102,6 +149,31 @@ namespace zaaReloaded2.ViewModels 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 #region Implementation ov ViewModelBase @@ -115,7 +187,8 @@ namespace zaaReloaded2.ViewModels #region Fields - SettingsViewModel _caller; + DelegatingCommand _chooseElementCommand; + Message _elementChosenMessage; #endregion }