Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Catel - Part 4 of n: Unit testing with Catel

, 28 Jan 2011
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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WindowExtensions.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Extensions for <see cref="System.Windows.Window" />.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using Catel.Windows.Properties;
using log4net;

namespace Catel.Windows
{
	/// <summary>
	/// Extensions for <see cref="System.Windows.Window"/>.
	/// </summary>
	public static class WindowExtensions
    {
        #region Variables
        /// <summary>
        /// The <see cref="ILog">log</see> object.
        /// </summary>
	    private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion

        #region Win32
        /// <summary>
		/// Extended windows styles.
		/// </summary>
		private const int GWL_EXSTYLE = -20;

		/// <summary>
		/// Window styles.
		/// </summary>
		private const int GWL_STYLE = -16;

		/// <summary>
		/// Shows or hides a system menu.
		/// </summary>
		private const int WS_SYSMENU = 0x80000;

		const int WS_EX_DLGMODALFRAME = 0x0001;

		const int SWP_NOSIZE = 0x0001;

		const int SWP_NOMOVE = 0x0002;

		const int SWP_NOZORDER = 0x0004;

		const int SWP_FRAMECHANGED = 0x0020;

		const uint WM_SETICON = 0x0080;

		[DllImport("user32.dll", SetLastError = true)]
		static extern int GetWindowLong(IntPtr hWnd, int nIndex);

		[DllImport("user32.dll")]
		static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

		[DllImport("user32.dll")]
		static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags);

		[DllImport("user32.dll")]
		static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

		[DllImport("user32.dll", SetLastError = true)]
		static extern bool BringWindowToTop(IntPtr hWnd);

		/// <summary>
		/// RECT struct for platform invokation.
		/// </summary>
		[StructLayout(LayoutKind.Sequential)]
		public struct RECT
		{
			/// <summary>
			/// Left.
			/// </summary>
			public int Left;

			/// <summary>
			/// Top.
			/// </summary>
			public int Top;

			/// <summary>
			/// Right.
			/// </summary>
			public int Right;

			/// <summary>
			/// Bottom.
			/// </summary>
			public int Bottom;
		}
		#endregion

		#region Current process main window
		/// <summary>
		/// Sets the owner window to the main window of the current process.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		public static void SetOwnerWindow(this Window window)
		{
			// First, try to get the main window
			if ((Application.Current != null) && (Application.Current.MainWindow != null))
			{
				// Set by main window
				SetOwnerWindowByWindow(window, Application.Current.MainWindow, false, false);
			}
			else
			{
				// Set by process main window handle
				SetOwnerWindowByHandle(window, GetProcessMainWindowHandle(), false, false);
			}
		}

		/// <summary>
		/// Sets the owner window to the main window of the current process, but
		/// also sets the focus on the first control.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		public static void SetOwnerWindowAndFocus(this Window window)
		{
			// First, try to get the main window
			if ((Application.Current != null) && (Application.Current.MainWindow != null) && Application.Current.MainWindow.IsVisible)
			{
				// Set by main window
				SetOwnerWindowByWindow(window, Application.Current.MainWindow, false, true);
			}
			else
			{
				// Set by process main window handle
				SetOwnerWindowByHandle(window, GetProcessMainWindowHandle(), false, true);
			}
		}

		/// <summary>
		/// Sets the owner window to the main window of the current process.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="forceNewOwner">If true, the new owner will be forced. Otherwise, if the
		/// window currently has an owner, that owner will be respected (and thus not changed).</param>
		public static void SetOwnerWindow(this Window window, bool forceNewOwner)
		{
			// First, try to get the main window
			if ((Application.Current != null) && (Application.Current.MainWindow != null))
			{
				// Set by main window
				SetOwnerWindowByWindow(window, Application.Current.MainWindow, forceNewOwner, false);
			}
			else
			{
				// Set by process main window handle
				SetOwnerWindowByHandle(window, GetProcessMainWindowHandle(), forceNewOwner, false);
			}
		}

		/// <summary>
		/// Sets the owner window to the main window of the current process, but
		/// also sets the focus on the first control.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="forceNewOwner">If true, the new owner will be forced. Otherwise, if the
		/// window currently has an owner, that owner will be respected (and thus not changed).</param>
		public static void SetOwnerWindowAndFocus(this Window window, bool forceNewOwner)
		{
			// First, try to get the main window
			if ((Application.Current != null) && (Application.Current.MainWindow != null))
			{
				// Set by main window
				SetOwnerWindowByWindow(window, Application.Current.MainWindow, forceNewOwner, true);
			}
			else
			{
				// Set by process main window handle
				SetOwnerWindowByHandle(window, GetProcessMainWindowHandle(), forceNewOwner, true);
			}
		}
		#endregion

		#region Specific window - Window
		/// <summary>
		/// Sets the owner window of a specific window via the Window class.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="owner">New owner window.</param>
		public static void SetOwnerWindow(this Window window, Window owner)
		{
			SetOwnerWindowByWindow(window, owner, false, false);
		}

		/// <summary>
		/// Sets the owner window of a specific window via the Window class, but
		/// also sets the focus on the first control.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="owner">New owner window.</param>
		public static void SetOwnerWindowAndFocus(this Window window, Window owner)
		{
			SetOwnerWindowByWindow(window, owner, false, true);
		}

		/// <summary>
		/// Sets the owner window of a specific window via the Window class.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="owner">New owner window.</param>
		/// <param name="forceNewOwner">If true, the new owner will be forced. Otherwise, if the
		/// window currently has an owner, that owner will be respected (and thus not changed).</param>
		public static void SetOwnerWindow(this Window window, Window owner, bool forceNewOwner)
		{
			SetOwnerWindowByWindow(window, owner, forceNewOwner, false);
		}

		/// <summary>
		/// Sets the owner window of a specific window via the Window class, but
		/// also sets the focus on the first control.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="owner">New owner window.</param>
		/// <param name="forceNewOwner">If true, the new owner will be forced. Otherwise, if the
		/// window currently has an owner, that owner will be respected (and thus not changed).</param>
		public static void SetOwnerWindowAndFocus(this Window window, Window owner, bool forceNewOwner)
		{
			SetOwnerWindowByWindow(window, owner, forceNewOwner, true);
		}

		/// <summary>
		/// Sets the owner window of a specific window.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="owner">New owner window.</param>
		/// <param name="forceNewOwner">If true, the new owner will be forced. Otherwise, if the
		/// window currently has an owner, that owner will be respected (and thus not changed).</param>
		/// <param name="focusFirstControl">If true, the first control will automatically be focused.</param>
		private static void SetOwnerWindowByWindow(Window window, Window owner, bool forceNewOwner, bool focusFirstControl)
		{
			SetOwnerWindow(window, owner, IntPtr.Zero, forceNewOwner, focusFirstControl);
		}
		#endregion

		#region Specific window - IntPtr
		/// <summary>
		/// Sets the owner window of a specific window via the window handle.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="owner">New owner window.</param>
		public static void SetOwnerWindow(this Window window, IntPtr owner)
		{
			SetOwnerWindowByHandle(window, owner, false, false);
		}

		/// <summary>
		/// Sets the owner window of a specific window via the window handle, but
		/// also sets the focus on the first control.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="owner">New owner window.</param>
		public static void SetOwnerWindowAndFocus(this Window window, IntPtr owner)
		{
			SetOwnerWindowByHandle(window, owner, false, true);
		}

		/// <summary>
		/// Sets the owner window of a specific window via the window handle.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="owner">New owner window.</param>
		/// <param name="forceNewOwner">If true, the new owner will be forced. Otherwise, if the
		/// window currently has an owner, that owner will be respected (and thus not changed).</param>
		public static void SetOwnerWindow(this Window window, IntPtr owner, bool forceNewOwner)
		{
			SetOwnerWindowByHandle(window, owner, forceNewOwner, false);
		}

		/// <summary>
		/// Sets the owner window of a specific window via the window handle, but
		/// also sets the focus on the first control.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="owner">New owner window.</param>
		/// <param name="forceNewOwner">If true, the new owner will be forced. Otherwise, if the
		/// window currently has an owner, that owner will be respected (and thus not changed).</param>
		public static void SetOwnerWindowAndFocus(this Window window, IntPtr owner, bool forceNewOwner)
		{
			SetOwnerWindowByHandle(window, owner, forceNewOwner, true);
		}

		/// <summary>
		/// Sets the owner window of a specific window via the window handle.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="owner">New owner window.</param>
		/// <param name="forceNewOwner">If true, the new owner will be forced. Otherwise, if the
		/// window currently has an owner, that owner will be respected (and thus not changed).</param>
		/// <param name="focusFirstControl">If true, the first control will automatically be focused.</param>
		private static void SetOwnerWindowByHandle(Window window, IntPtr owner, bool forceNewOwner, bool focusFirstControl)
		{
			SetOwnerWindow(window, null, owner, forceNewOwner, focusFirstControl);
		}
		#endregion

		/// <summary>
		/// Brings to specified window to top.
		/// </summary>
		/// <param name="window">The window to bring to top.</param>
		/// <exception cref="ArgumentNullException">when <paramref name="window"/> is <c>null</c>.</exception>
		public static void BringWindowToTop(this Window window)
		{
			if (window == null)
			{
			    throw new ArgumentNullException("window");
			}

			WindowInteropHelper interopHelper = new WindowInteropHelper(window);
			BringWindowToTop(interopHelper.Handle);
		}

		/// <summary>
		/// Sets the owner window of a specific window. It will first try to set the owner via
		/// the <paramref name="ownerWindow"/>. If the <paramref name="ownerWindow"/> is not available,
		/// this method will use the <paramref name="ownerHandle"/> to set the parent.
		/// </summary>
		/// <param name="window">Reference to the current window.</param>
		/// <param name="ownerWindow">New owner window.</param>
		/// <param name="ownerHandle">The owner handle.</param>
		/// <param name="forceNewOwner">If true, the new owner will be forced. Otherwise, if the
		/// window currently has an owner, that owner will be respected (and thus not changed).</param>
		/// <param name="focusFirstControl">If true, the first control will automatically be focused.</param>
		private static void SetOwnerWindow(Window window, Window ownerWindow, IntPtr ownerHandle, bool forceNewOwner, bool focusFirstControl)
		{
			if (focusFirstControl)
			{
			    window.FocusFirstControl();
			}

			if (!forceNewOwner && HasOwner(window))
			{
			    return;
			}

			if (ownerWindow != null)
			{
                if (ownerWindow == window)
                {
                    Log.Warn(TraceMessages.CannotSetOwnerWindowToItself);
                    return;
                }

				window.Owner = ownerWindow;
			}
			else
			{
				// Set owner via interop helper
				WindowInteropHelper interopHelper = new WindowInteropHelper(window);
				interopHelper.Owner = ownerHandle;

				// Since this owner type doesn't support WindowStartupLocation.CenterOwner, do
				// it manually
				if (window.WindowStartupLocation == WindowStartupLocation.CenterOwner)
				{
					// Get handler (so we can nicely unsubscribe)
					RoutedEventHandler window_Loaded = null;
					window_Loaded = delegate(object sender, RoutedEventArgs e)
					{
						// Get the parent window rect
						RECT ownerRect;
						if (GetWindowRect(ownerHandle, out ownerRect))
						{
							// Get some additional information
							int ownerWidth = ownerRect.Right - ownerRect.Left;
							int ownerHeight = ownerRect.Bottom - ownerRect.Top;
							int ownerHorizontalCenter = (ownerWidth / 2) + ownerRect.Left;
							int ownerVerticalCenter = (ownerHeight / 2) + ownerRect.Top;

							// Set the location to manual
							window.WindowStartupLocation = WindowStartupLocation.Manual;

							// Now we know the location of the parent, center the window
							window.Left = ownerHorizontalCenter - (window.ActualWidth / 2);
							window.Top = ownerVerticalCenter - (window.ActualHeight / 2);
						}

						((Window)sender).Loaded -= window_Loaded;
					};

					window.Loaded += window_Loaded;
				}
			}
		}

		/// <summary>
		/// Returns the main window handle of the current process.
		/// </summary>
		/// <returns>Handle of the main window of the current process.</returns>
		private static IntPtr GetProcessMainWindowHandle()
		{
			return System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
		}

		/// <summary>
		/// Returns whether the window currently has an owner.
		/// </summary>
		/// <param name="window">Window to check.</param>
		/// <returns>
		/// True if the window has an owner, otherwise false.
		/// </returns>
		private static bool HasOwner(Window window)
		{
			return ((window.Owner != null) || (new WindowInteropHelper(window).Owner != IntPtr.Zero));
		}

		/// <summary>
		/// Removes the icon from the window.
		/// </summary>
		/// <param name="window">The window.</param>
		public static void RemoveIcon(this Window window)
		{
			// Get the handle of the window
			IntPtr windowHandle = new WindowInteropHelper(window).Handle;

			// Send message to hide icon
			SendMessage(windowHandle, WM_SETICON, IntPtr.Zero, IntPtr.Zero);

			// Change the extended window style to not show a window icon
			int extendedStyle = GetWindowLong(windowHandle, GWL_EXSTYLE);

			// Update the window style
			SetWindowLong(windowHandle, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);

			// Update the window's non-client area to reflect the changes
			SetWindowPos(windowHandle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
		}
	}
}

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
Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 28 Jan 2011
Article Copyright 2011 by Geert van Horrik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid