Click here to Skip to main content
11,704,315 members (60,977 online)
Click here to Skip to main content
Add your own
alternative version

Catel - Part 4 of n: Unit testing with Catel

, 28 Jan 2011 CPOL 20.2K 485 9
This article explains how to write unit tests for MVVM using Catel.
Catel-04_01-unittesting.zip
src
Catel.Articles.04 - Unit testing
Models
Properties
Settings.settings
Resources
Images
add.png
delete.png
edit.png
group.png
UI
Data
Converters
ViewModels
Windows
Catel.Articles.04 - Unit testing.Test
Models
Properties
UI
ViewModels
Catel.Articles.Base
Data
Attributes
Properties
Settings.settings
Resources
Images
CatenaLogic.png
Preview.png
Run.png
ShowCode.png
UI
Controls
Helpers
ViewModels
Windows
Catel.Core
Attributes
ClassDiagrams
DataObjectBase.cd
SavableDataObjectBase.cd
Collections
Helpers
ComponentModel
Data
Attributes
Exceptions
Interfaces
Diagnostics
Extensions
Helpers
Exceptions
Helpers
IO
Exceptions
IoC
LLBLGen
Log4net
Appender
Extensions
Helpers
MVVM
Commands
Interfaces
Exceptions
Services
EventArgs
Exceptions
Interfaces
ViewModels
Attributes
Interfaces
Properties
Reflection
Exceptions
Extensions
Helpers
Runtime
Serialization
Attributes
Helpers
Security
Cryptography
Helpers
Catel.Examples.Models
Properties
Catel.Examples.PersonApplication
Properties
Settings.settings
Resources
Images
add.png
delete.png
edit.png
group.png
UI
Data
Converters
ViewModels
Windows
Catel.Examples.Silverlight
Properties
Resources
Images
add.png
delete.png
edit.png
group.png
UI
Data
Converters
Pages
ViewModels
Windows
Catel.Examples.Silverlight.Web
Catel.Examples.Silverlight.Web.csproj.user
ClientBin
Properties
Catel.FxCop
Catel.Silverlight
Diagnostics
log4net
Core
MVVM
Commands
Services
ViewModels
Properties
Catel.Core
Catel.Windows
Reflection
Themes
Generic
Assets
Old
Windows
Controls
Data
Converters
Helpers
Helpers
Catel.Silverlight.Test
Properties
Catel.Silverlight.Test.Web
Catel.Silverlight.Test.Web.csproj.user
ClientBin
Properties
Catel.snk
Catel.Templates.WpfApplication
Properties
Settings.settings
UI
Controls
ViewModels
Windows
Catel.Templates.WpfItemTemplates
Properties
UI
Controls
ViewModels
Windows
Catel.Test
Collections
Convert
Data
Helpers
IO
MVVM
UI
ViewModels
Properties
Reflection
Runtime
Serialization
Security
Cryptography
Test References
Catel.Windows.accessor
Windows
Data
Converters
Catel.vsmdi
Catel.Windows
ClassDiagrams
ViewModelBase.cd
Collections
Extensions
Helpers
MVVM
Commands
Services
Test
UI
ViewModels
Properties
Settings.settings
Resources
Images
Add.png
ClearOutput.png
Edit.png
Error.png
Loading.gif
Preview.png
Remove.png
Save.png
TipOfTheDay.png
Warning.png
Themes
Aero
ExpressionDark
Assets
Generic
Assets
Controls
Jetpack
Assets
background.png
Old
SunnyOrange
Assets
Windows
Controls
Extensions
LinkLabel
StackGrid
Data
Converters
Helpers
Documents
Extensions
Extensions
Helpers
Input
Markup
Media
Effects
EmptyEffect
EmptyEffect.fx
EmptyEffect.ps
EmptyEffect.fx
GrayscaleEffect
GrayscaleEffect.fx
GrayscaleEffect.ps
Extensions
Imaging
Extensions
Windows
DataWindow
TipOfTheDay
Local.testsettings
Settings.StyleCop
TraceAndTestImpact.testsettings
using System;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows;
using Catel.Articles.Base.Data;
using Catel.Articles.Base.Data.Attributes;
using Catel.Articles.Base.Properties;
using Catel.Data;
using Catel.MVVM;
using Catel.MVVM.Services;
using log4net;

namespace Catel.Articles.Base.UI.ViewModels
{
    /// <summary>
    /// Examples view model.
    /// </summary>
    public class ExamplesViewModel : ViewModelBase
	{
		#region Variables
    	private readonly string _title;
		#endregion

		#region Constructor & destructor
		/// <summary>
		/// Initializes a new instance of the <see cref="ExamplesViewModel"/> class.
		/// </summary>
		/// <param name="title">The title.</param>
		public ExamplesViewModel(string title)
		{
			// Store values
			_title = title;
		}
		#endregion

		#region Properties
    	/// <summary>
    	/// Gets the title of the view model.
    	/// </summary>
    	/// <value>The title.</value>
    	public override string Title { get { return _title; } }

        #region View model
        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        public string Description
        {
            get { return GetValue<string>(DescriptionProperty); }
            set { SetValue(DescriptionProperty, value); }
        }

        /// <summary>
        /// Register the Description property so it is known in the class.
        /// </summary>
        public static readonly PropertyData DescriptionProperty = RegisterProperty("Description", typeof(string), Resources.ExamplesDescription);

        /// <summary>
        /// Gets or sets a collection of themes.
        /// </summary>
        public Collection<ThemeInfo> ThemeCollection
        {
            get { return GetValue<Collection<ThemeInfo>>(ThemeCollectionProperty); }
            set { SetValue(ThemeCollectionProperty, value); }
        }

        /// <summary>
        /// Register the ThemeCollection property so it is known in the class.
        /// </summary>
        public static readonly PropertyData ThemeCollectionProperty = RegisterProperty("ThemeCollection", typeof(Collection<ThemeInfo>));

        /// <summary>
        /// Gets or sets the selected theme.
        /// </summary>
        public ThemeInfo SelectedTheme
        {
            get { return GetValue<ThemeInfo>(SelectedThemeProperty); }
            set { SetValue(SelectedThemeProperty, value); }
        }

        /// <summary>
        /// Register the SelectedTheme property so it is known in the class.
        /// </summary>
        public static readonly PropertyData SelectedThemeProperty = RegisterProperty("SelectedTheme", typeof(ThemeInfo),
            (sender, e) => ((ExamplesViewModel)sender).OnSelectedThemeChanged());

        /// <summary>
        /// Gets or sets the examples collection.
        /// </summary>
        public ObservableCollection<ExampleInfo> Examples
        {
            get { return GetValue<ObservableCollection<ExampleInfo>>(ExamplesProperty); }
            set { SetValue(ExamplesProperty, value); }
        }

        /// <summary>
        /// Register the Examples property so it is known in the class.
        /// </summary>
        public static readonly PropertyData ExamplesProperty = RegisterProperty("Examples", typeof(ObservableCollection<ExampleInfo>), new ObservableCollection<ExampleInfo>());

        /// <summary>
        /// Gets or sets the selected example.
        /// </summary>
        public ExampleInfo SelectedExample
        {
            get { return GetValue<ExampleInfo>(SelectedExampleProperty); }
            set { SetValue(SelectedExampleProperty, value); }
        }

        /// <summary>
        /// Register the SelectedExample property so it is known in the class.
        /// </summary>
        public static readonly PropertyData SelectedExampleProperty = RegisterProperty("SelectedExample", typeof(ExampleInfo));
        #endregion
        #endregion

        #region Methods
        /// <summary>
        /// Initializes the object by setting default values.
        /// </summary>	
        protected override void Initialize()
        {
            // Log
            Log.Info("Welcome to the examples app");
            Log.StartStopwatchTrace("Searching for all examples in the current AppDomain");

            // Get all the types of the app domain
            foreach (Assembly assembly in AssemblyHelper.GetLoadedAssemblies())
            {
                try
                {
                    foreach (Type type in assembly.GetTypes())
                    {
                        // Is the type decorated with the example attribute?
                        if (type.IsClass)
                        {
                            ExampleAttribute[] exampleAttributes = (ExampleAttribute[])type.GetCustomAttributes(typeof(ExampleAttribute), false);
                            if (exampleAttributes.Length > 0)
                            {
                                // Create example info
                                ExampleInfo exampleInfo = new ExampleInfo(exampleAttributes[0]);

                                // Does the attribute have a click handler?
                                if (!string.IsNullOrEmpty(exampleAttributes[0].ClickHandlerName))
                                {
                                    // Try to find the handler
                                    MethodInfo methodInfo = type.GetMethod(exampleAttributes[0].ClickHandlerName,
                                        BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                                    if (methodInfo != null)
                                    {
                                        // Store it
                                        exampleInfo.ClickHandler = Delegate.CreateDelegate(typeof(VoidDelegate), methodInfo) as VoidDelegate;
                                    }
                                }

                                // Add
                                if (!Examples.Contains(exampleInfo)) Examples.Add(exampleInfo);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Log and continue
                    Log.Error(ex, "Failed to get types of an assembly");
                }
            }

            // Log
            Log.StopStopwatchTrace("Searching for all examples in the current AppDomain");
            Log.Info("Found '{0}' example(s)", Examples.Count);

            // Set initial example
            if (Examples.Count > 0) SelectedExample = Examples[0];

            // Initialize themes
            ThemeCollection = new Collection<ThemeInfo>();
            ThemeCollection.Add(new ThemeInfo("Aero - normal", "/Catel.Windows;component/themes/aero/catel.aero.normal.xaml"));
            ThemeCollection.Add(new ThemeInfo("Aero - large", "/Catel.Windows;component/themes/aero/catel.aero.large.xaml"));
            ThemeCollection.Add(new ThemeInfo("Expression Dark - normal", "/Catel.Windows;component/themes/expressiondark/catel.expressiondark.normal.xaml"));
            ThemeCollection.Add(new ThemeInfo("Expression Dark - large", "/Catel.Windows;component/themes/expressiondark/catel.expressiondark.large.xaml"));
            ThemeCollection.Add(new ThemeInfo("Jetpack", "/Catel.Windows;component/themes/jetpack/generic.xaml"));
            ThemeCollection.Add(new ThemeInfo("Sunny Orange", "/Catel.Windows;component/themes/sunnyorange/generic.xaml"));

            // Set selected (default) theme
            SelectedTheme = ThemeCollection[0];
        }

        /// <summary>
        /// Called when the <see cref="SelectedTheme"/> property has changed.
        /// </summary>
        private void OnSelectedThemeChanged()
        {
            // Clear app resources
            var currentApp = Application.Current;
            if (currentApp == null)
            {
                // Show error
                var messageService = GetService<IMessageService>();
                messageService.ShowError("There is no current application object, cannot switch theme");

                // Exit
                return;
            }

            // Need to call this twice because the first update fixes the dictionaries, and the second one actually updates the UI
            UpdateApplicationResources(currentApp);
            UpdateApplicationResources(currentApp);
        }

        /// <summary>
        /// Updates the application resources.
        /// </summary>
        /// <param name="currentApp">The current application.</param>
        private void UpdateApplicationResources(Application currentApp)
        {
            // Clear resources
            currentApp.Resources.Clear();
            currentApp.Resources.MergedDictionaries.Clear();

            // Merge resources
            ResourceDictionary resourceDictionary = new ResourceDictionary(); 
            resourceDictionary.Source = new Uri(SelectedTheme.Source, UriKind.RelativeOrAbsolute);

            // Now merge the dictionaries
            currentApp.Resources.MergedDictionaries.Add(resourceDictionary);

            // Create style forwarders
            Catel.Windows.StyleHelper.CreateStyleForwardersForDefaultStyles();
        }
        #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)

Share

About the Author

Geert van Horrik
Software Developer CatenaLogic
Netherlands Netherlands
I am Geert van Horrik, and I have studied Computer Science in the Netherlands.

I love to write software using .NET (especially the combination of WPF and C#). I am also the lead developer of Catel, an open-source application development framework for WPF, Silverlight, WP7 and WinRT with the focus on MVVM.

I have my own company since January 1st 2007, called CatenaLogic. This company develops commercial and non-commercial software.

To download (or buy) applications I have written, visit my website: http://www.catenalogic.com

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 28 Jan 2011
Article Copyright 2011 by Geert van Horrik
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid