Click here to Skip to main content
15,891,734 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 49.1K   572   11  
This article explains how to write unit tests for MVVM using Catel.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TipOfTheDayEditorWindow.xaml.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Interaction logic for TipOfTheDayEditorWindow.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.Windows;
using System.Windows.Input;
using Catel.Windows.Data;
using Catel.Windows.Input;

namespace Catel.Windows
{
    /// <summary>
    /// Interaction logic for TipOfTheDayEditorWindow.xaml
    /// </summary>
    public partial class TipOfTheDayEditorWindow : Window
    {
        #region Variables
        #endregion

        #region Constructor & destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="TipOfTheDayEditorWindow"/> class.
        /// </summary>
        public TipOfTheDayEditorWindow()
        {
            InitializeComponent();

            CommandBindings.Add(new CommandBinding(DataCommands.Add, Add_Executed, Add_CanExecute));
            CommandBindings.Add(new CommandBinding(DataCommands.Edit, Edit_Executed, Edit_CanExecute));
            CommandBindings.Add(new CommandBinding(DataCommands.Remove, Remove_Executed, Remove_CanExecute));
            CommandBindings.Add(new CommandBinding(TipOfTheDayCommands.Preview, Preview_Executed, Preview_CanExecute));
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Save, Save_Executed, Save_CanExecute));
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, Close_Executed, Close_CanExecute));

            TipOfTheDay = TipOfTheDayData.Load();

            this.SetOwnerWindow();
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets TipOfTheDay.
        /// </summary>
        /// <remarks>
        /// Wrapper for the TipOfTheDay dependency property.
        /// </remarks>
        public TipOfTheDayData TipOfTheDay
        {
            get { return (TipOfTheDayData)GetValue(TipOfTheDayProperty); }
            private set { SetValue(TipOfTheDayPropertyKey, value); }
        }

        /// <summary>
        /// Definition of the dependency property is private.
        /// </summary>
        private static readonly DependencyPropertyKey TipOfTheDayPropertyKey =
            DependencyProperty.RegisterReadOnly("TipOfTheDay", typeof(TipOfTheDayData), typeof(TipOfTheDayEditorWindow), new UIPropertyMetadata(null));

        /// <summary>
        /// Read-only dependencyProperty definition as the backing store for TipOfTheDay.
        /// </summary>
        public static readonly DependencyProperty TipOfTheDayProperty = TipOfTheDayPropertyKey.DependencyProperty;

        /// <summary>
        /// Gets or sets SelectedTipOfTheDay.
        /// </summary>
        /// <remarks>
        /// Wrapper for the SelectedTipOfTheDay dependency property.
        /// </remarks>
        public TipOfTheDayItem SelectedTipOfTheDay
        {
            get { return (TipOfTheDayItem)GetValue(SelectedTipOfTheDayProperty); }
            set { SetValue(SelectedTipOfTheDayProperty, value); }
        }

        /// <summary>
        /// DependencyProperty definition as the backing store for SelectedTipOfTheDay.
        /// </summary>
        public static readonly DependencyProperty SelectedTipOfTheDayProperty =
            DependencyProperty.Register("SelectedTipOfTheDay", typeof(TipOfTheDayItem), typeof(TipOfTheDayEditorWindow), new UIPropertyMetadata(null));
        #endregion

        #region Command bindings
        /// <summary>
        /// Determines whether the user can execute the Add command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">The event data.</param>
        private void Add_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        /// <summary>
        /// Handled when the user invokes the Add command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Event Arguments.</param>
        private void Add_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            TipOfTheDayItem tipOfTheDayElement = new TipOfTheDayItem();
            TipOfTheDayItemEditorWindow tipOfTheDayElementEditorWindow = new TipOfTheDayItemEditorWindow(tipOfTheDayElement);
            if (tipOfTheDayElementEditorWindow.ShowDialog() ?? false)
            {
                TipOfTheDay.Tips.Add(tipOfTheDayElement);
            }
        }

        /// <summary>
        /// Determines whether the user can execute the Edit command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">The event data.</param>
        private void Edit_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (SelectedTipOfTheDay == null)
            {
                return;
            }

            e.CanExecute = true;
        }

        /// <summary>
        /// Handled when the user invokes the Edit command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Event Arguments.</param>
        private void Edit_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            TipOfTheDayItemEditorWindow tipOfTheDayElementEditorWindow = new TipOfTheDayItemEditorWindow(SelectedTipOfTheDay);
            tipOfTheDayElementEditorWindow.ShowDialog();
        }

        /// <summary>
        /// Determines whether the user can execute the Remove command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">The event data.</param>
        private void Remove_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (SelectedTipOfTheDay == null)
            {
                return;
            }

            e.CanExecute = true;
        }

        /// <summary>
        /// Handled when the user invokes the Remove command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Event Arguments.</param>
        private void Remove_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            TipOfTheDay.Tips.Remove(SelectedTipOfTheDay);

            SelectedTipOfTheDay = null;
        }

        /// <summary>
        /// Determines whether the user can execute the Preview command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">The event data.</param>
        private void Preview_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (SelectedTipOfTheDay == null)
            {
                return;
            }

            e.CanExecute = true;
        }

        /// <summary>
        /// Handled when the user invokes the Preview command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Event Arguments.</param>
        private void Preview_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            TipOfTheDayWindow tipOfTheDayWindow = new TipOfTheDayWindow(SelectedTipOfTheDay);
            tipOfTheDayWindow.ShowDialog();
        }

        /// <summary>
        /// Determines whether the user can execute the Save command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">The event data.</param>
        private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        /// <summary>
        /// Handled when the user invokes the Save command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Event Arguments.</param>
        private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            TipOfTheDay.Save();
        }

        /// <summary>
        /// Determines whether the user can execute the Close command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">The event data.</param>
        private void Close_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        /// <summary>
        /// Handled when the user invokes the Close command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Event Arguments.</param>
        private void Close_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (MessageBox.Show(Properties.Resources.ConfirmCloseTipOfTheDayEditor, Properties.Resources.ConfirmationTitle,
                MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
            {
                Close();
            }
        }
        #endregion

        #region Methods
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions