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

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="ModalWindowHelper.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Modal window helper class
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace Catel.Windows
{
	/// <summary>
	/// Modal window helper class
	/// </summary>
    public class ModalWindowHelper : IDisposable
    {
        #region Win32 Imports
        [DllImport("user32.dll", SetLastError = true)]
		private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);

        [DllImport("user32.dll")]
        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

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

		/// <summary>
		/// Win32 RECT structure.
		/// </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;
        }

        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
        static readonly IntPtr HWND_TOP = new IntPtr(0);
        static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
        #endregion

        #region Variables
        private bool _succeeded = false;
        private IntPtr _parentWindow = IntPtr.Zero;
        private string _className = string.Empty;
        private string _windowTitle = string.Empty;
        private Thread _thread = null;
        private DateTime _threadStopTime = DateTime.Now;
        #endregion

        #region Constructor & destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="ModalWindowHelper"/> class.
        /// </summary>
        /// <param name="parentWindow">Handle of the window to set as parent.</param>
        /// <param name="className">Class name of the window to make modal.</param>
        /// <param name="windowTitle">Title of the window to make modal.</param>
        public ModalWindowHelper(IntPtr parentWindow, string className, string windowTitle)
        {
            _parentWindow = parentWindow;
            _className = className;
            _windowTitle = windowTitle;

            StartSearch();
        }

        /// <summary>
        /// Finalizes an instance of the <see cref="ModalWindowHelper"/> class.
        /// </summary>
        ~ModalWindowHelper()
        {
            Dispose();
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            _succeeded = false;

            if (_thread != null)
            {
                _thread.Abort();
                _thread = null;
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets whether the window was found and its state is set to model.
        /// </summary>
        public bool IsSucceeded
        {
            get { return _succeeded; }
            private set { _succeeded = value; }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Starts the search for the window
        /// </summary>
        private void StartSearch()
        {
            Dispose();

            _thread = new Thread(FindWindowAndSetModal);
            _thread.Start();

            _threadStopTime = DateTime.Now.AddSeconds(30);
        }

        /// <summary>
        /// Tries to find the window and set its state to modal.
        /// </summary>
        private void FindWindowAndSetModal()
        {
            while (!_succeeded && (_threadStopTime > DateTime.Now))
            {
                IntPtr windowHandle = IntPtr.Zero;
                if (string.IsNullOrEmpty(_windowTitle))
                {
                    windowHandle = FindWindowEx(IntPtr.Zero, IntPtr.Zero, _className, IntPtr.Zero);
                }
                else
                {
                    windowHandle = FindWindowEx(IntPtr.Zero, IntPtr.Zero, _className, _windowTitle);
                }

                if (windowHandle != IntPtr.Zero)
                {
                    if (_parentWindow != IntPtr.Zero)
                    {
                        SetParent(windowHandle, _parentWindow);
                        _succeeded = true;
                    }
                    else
                    {
                        RECT rect;
                        GetWindowRect(windowHandle, out rect);

                        // Set the state to system modal
                        SetWindowPos(windowHandle, HWND_TOPMOST, rect.Left, rect.Top,
                            rect.Right - rect.Left, rect.Bottom - rect.Top, 0);

                        _succeeded = true;
                    }
                }

                Thread.Sleep(500);
            }
        }
        #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
Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.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