Click here to Skip to main content
15,892,809 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="MessageService.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Message service that implements the <see cref="IMessageService" /> by using the <see cref="MessageBox" /> class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Windows;
using Catel.Windows.Properties;

namespace Catel.MVVM.Services
{
	/// <summary>
	/// Message service that implements the <see cref="IMessageService"/> by using the <see cref="MessageBox"/> class.
	/// </summary>
	public class MessageService : ViewModelServiceBase, IMessageService
	{
		#region Methods
		/// <summary>
		/// Translates the message box result.
		/// </summary>
		/// <param name="result">The result.</param>
		/// <returns>
		/// Corresponding <see cref="MessageResult"/>.
		/// </returns>
		private static MessageResult TranslateMessageBoxResult(MessageBoxResult result)
		{
			switch (result)
			{
				case MessageBoxResult.None:
                    return MessageResult.None;

				case MessageBoxResult.OK:
                    return MessageResult.OK;

				case MessageBoxResult.Cancel:
					return MessageResult.Cancel;

				case MessageBoxResult.Yes:
					return MessageResult.Yes;

				case MessageBoxResult.No:
                    return MessageResult.No;

				default:
					throw new ArgumentOutOfRangeException("result");
			}
		}

		/// <summary>
		/// Translates the message image.
		/// </summary>
		/// <param name="image">The image.</param>
		/// <returns>
		/// Corresponding <see cref="MessageBoxImage"/>.
		/// </returns>
		private static MessageBoxImage TranslateMessageImage(MessageImage image)
		{
			switch (image)
			{
				case MessageImage.None:
                    return MessageBoxImage.None;

				case MessageImage.Information:
                    return MessageBoxImage.Information;

				case MessageImage.Question:
					return MessageBoxImage.Question;

				case MessageImage.Exclamation:
					return MessageBoxImage.Exclamation;

				case MessageImage.Error:
                    return MessageBoxImage.Error;

				case MessageImage.Stop:
					return MessageBoxImage.Stop;

				case MessageImage.Warning:
					return MessageBoxImage.Warning;

				default:
					throw new ArgumentOutOfRangeException("image");
			}
		}

		/// <summary>
		/// Translates the message button.
		/// </summary>
		/// <param name="button">The button.</param>
		/// <returns>
		/// Corresponding <see cref="MessageBoxButton"/>.
		/// </returns>
		private static MessageBoxButton TranslateMessageButton(MessageButton button)
		{
			switch (button)
			{
				case MessageButton.OK:
					return MessageBoxButton.OK;

				case MessageButton.OKCancel:
					return MessageBoxButton.OKCancel;

				case MessageButton.YesNo:
					return MessageBoxButton.YesNo;

				case MessageButton.YesNoCancel:
					return MessageBoxButton.YesNoCancel;

				default:
					throw new ArgumentOutOfRangeException("button");
			}
		}
		#endregion

		#region IMessageService Members
        /// <summary>
        /// Shows the error.
        /// </summary>
        /// <param name="exception">The exception.</param>
		public void ShowError(Exception exception)
		{
			ShowError(exception.Message, Resources.ErrorTitle);
		}

		/// <summary>
		/// Shows the error.
		/// </summary>
		/// <param name="message">The message.</param>
		public void ShowError(string message)
		{
			ShowError(message, Resources.ErrorTitle);
		}

		/// <summary>
		/// Shows the error.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="caption">The caption.</param>
		public void ShowError(string message, string caption)
		{
			Show(message, caption, MessageButton.OK, MessageImage.Error);
		}

		/// <summary>
		/// Shows the warning.
		/// </summary>
		/// <param name="message">The message.</param>
		public void ShowWarning(string message)
		{
			ShowWarning(message, Resources.WarningTitle);
		}

		/// <summary>
		/// Shows the warning.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="caption">The caption.</param>
		public void ShowWarning(string message, string caption)
		{
			Show(message, caption, MessageButton.OK, MessageImage.Warning);
		}

		/// <summary>
		/// Shows the information.
		/// </summary>
		/// <param name="message">The message.</param>
		public void ShowInformation(string message)
		{
			ShowInformation(message, Resources.InfoTitle);
		}

		/// <summary>
		/// Shows the information.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="caption">The caption.</param>
		public void ShowInformation(string message, string caption)
		{
			Show(message, caption, MessageButton.OK, MessageImage.Information);
		}

		/// <summary>
		/// Shows the specified message.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <returns>The <see cref="MessageResult"/>.</returns>
		public MessageResult Show(string message)
		{
			return Show(message, Resources.InfoTitle);
		}

		/// <summary>
		/// Shows the specified message.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="caption">The caption.</param>
		/// <returns>The <see cref="MessageResult"/>.</returns>
		public MessageResult Show(string message, string caption)
		{
			return Show(message, caption, MessageButton.OK);
		}

		/// <summary>
		/// Shows the specified message.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="caption">The caption.</param>
		/// <param name="button">The button.</param>
		/// <returns>The <see cref="MessageResult"/>.</returns>
		public MessageResult Show(string message, string caption, MessageButton button)
		{
			return Show(message, caption, button, MessageImage.None);
		}

		/// <summary>
		/// Shows the specified message.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="caption">The caption.</param>
		/// <param name="button">The button.</param>
		/// <param name="icon">The icon to show.</param>
		/// <returns>The <see cref="MessageResult"/>.</returns>
		public MessageResult Show(string message, string caption, MessageButton button, MessageImage icon)
		{
			var messageBoxButton = TranslateMessageButton(button);
			var messageBoxImage = TranslateMessageImage(icon);
			
			var result = MessageBox.Show(message, caption, messageBoxButton, messageBoxImage);

			return TranslateMessageBoxResult(result);
		}
	    #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