Click here to Skip to main content
15,891,863 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.
using System;
using System.Windows;
using System.Windows.Controls;
using Catel.Articles.Base.UI.Windows;

namespace Catel.Articles.Base.UI
{
    /// <summary>
    /// Example helper.
    /// </summary>
    public static class ExampleHelper
    {
        /// <summary>
        /// Shows a control in a separate window.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        /// <exception cref="ArgumentNullException">when <paramref name="frameworkElement"/> is <c>null</c>.</exception>
        public static void ShowControlInWindow(FrameworkElement frameworkElement)
        {
            ShowControlInWindow(frameworkElement, null);
        }

        /// <summary>
        /// Shows a control in a separate window.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        /// <param name="labelText">The label text to show in front of the control. If <see cref="string.Empty"/> or <c>null</c>, no label will be prefixed.</param>
        /// <exception cref="ArgumentNullException">when <paramref name="frameworkElement"/> is <c>null</c>.</exception>
        public static void ShowControlInWindow(FrameworkElement frameworkElement, string labelText)
        {
            ShowControlInWindow(frameworkElement, labelText, 0, 0);
        }

        /// <summary>
        /// Shows a control in a separate window.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        /// <param name="labelText">The label text to show in front of the control. If <see cref="string.Empty"/> or <c>null</c>, no label will be prefixed.</param>
        /// <param name="minWidth">Minimum width of the control. If smaller or equal to 0, no minimum width is specified.</param>
        /// <param name="maxWidth">Maximum width of the control. If smaller or equal to 0, no maximum width is specified.</param>
        /// <exception cref="ArgumentNullException">when <paramref name="frameworkElement"/> is <c>null</c>.</exception>
        public static void ShowControlInWindow(FrameworkElement frameworkElement, string labelText, int minWidth, int maxWidth)
        {
            ShowControlInWindow(frameworkElement, labelText, minWidth, maxWidth, 0, 0);
        }

        /// <summary>
        /// Shows a control in a separate window.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        /// <param name="labelText">The label text to show in front of the control. If <see cref="string.Empty"/> or <c>null</c>, no label will be prefixed.</param>
        /// <param name="minWidth">Minimum width of the control. If smaller or equal to 0, no minimum width is specified.</param>
        /// <param name="maxWidth">Maximum width of the control. If smaller or equal to 0, no maximum width is specified.</param>
        /// <param name="minHeight">Minimum height of the control. If smaller or equal to 0, no minimum height is specified.</param>
        /// <param name="maxHeight">Maximum height of the control. If smaller or equal to 0, no maximum height is specified.</param>
        /// <exception cref="ArgumentNullException">when <paramref name="frameworkElement"/> is <c>null</c>.</exception>
        public static void ShowControlInWindow(FrameworkElement frameworkElement, string labelText, int minWidth, int maxWidth, int minHeight, int maxHeight)
        {
            // Validate input
            if (frameworkElement == null) throw new ArgumentNullException("frameworkElement");

            // Create container
            Grid container = new Grid();

            // Row definitions
            RowDefinition firstRow = new RowDefinition();
            firstRow.Height = new GridLength(1, GridUnitType.Auto);
            if (minHeight > 0) firstRow.MinHeight = minHeight;
            if (maxHeight > 0) firstRow.MaxHeight = maxHeight;
            container.RowDefinitions.Add(firstRow);

            // Column definitions
            ColumnDefinition firstColumn = new ColumnDefinition();
            firstColumn.Width = new GridLength(1, GridUnitType.Auto);
            container.ColumnDefinitions.Add(firstColumn);
            ColumnDefinition secondColumn = new ColumnDefinition();
            firstColumn.Width = new GridLength(1, GridUnitType.Star);
            if (minWidth > 0) secondColumn.MinWidth = minWidth;
            if (maxWidth > 0) secondColumn.MaxWidth = maxWidth;
            container.ColumnDefinitions.Add(secondColumn);

            // Should we add a label?);
            if (!string.IsNullOrEmpty(labelText))
            {
                // Yes, add it
                Label label = new Label();
                label.Content = labelText;
                label.SetValue(Grid.ColumnProperty, 0);
                container.Children.Add(label);
            }

            // Add control
            frameworkElement.SetValue(Grid.ColumnProperty, 1);
            container.Children.Add(frameworkElement);

            // Show container in window
            ControlExampleWindow controlExampleWindow = new ControlExampleWindow();
            controlExampleWindow.LoadFrameworkElement(container);

            // Show window
            controlExampleWindow.ShowDialog();
        }
    }
}

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