Click here to Skip to main content
15,886,199 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 48.9K   572   11  
This article explains how to write unit tests for MVVM using Catel.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Catel.Properties;

namespace Catel.Windows
{
    /// <summary>
    /// Internal "replacement" for the <c>LogicalTreeHelper</c> as it can be found in WPF.
    /// </summary>
    public static class LogicalTreeHelper
    {
        /// <summary>
        /// Finds a logical node in the tree of the specified <see cref="DependencyObject"/>.
        /// </summary>
        /// <param name="dependencyObject">The dependency object.</param>
        /// <param name="name">The name of the control to find.</param>
        /// <returns>Child as <see cref="DependencyObject"/> or <c>null</c> if the child cannot be found.</returns>
        /// <exception cref="ArgumentNullException">when <paramref name="dependencyObject"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">when <paramref name="name"/> is <c>null</c> or empty.</exception>
        public static DependencyObject FindLogicalNode(DependencyObject dependencyObject, string name)
        {
            if (dependencyObject == null)
            {
                throw new ArgumentNullException("dependencyObject");
            }

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(Exceptions.ArgumentCannotBeNullOrEmpty, "name");
            }

            // Check if the object is the node itself
            if (IsElementWithName(dependencyObject, name))
            {
                return dependencyObject;
            }

            // Search all child nodes
            List<DependencyObject> children = new List<DependencyObject>(dependencyObject.GetVisualChildren());
            foreach (DependencyObject child in children)
            {
                if (IsElementWithName(child, name))
                {
                    return child;
                }
            }

            // Since we didn't find anything, check the childs of all childs
            return children.Select(child => FindLogicalNode(child, name)).FirstOrDefault(foundChild => foundChild != null);
        }

        /// <summary>
        /// Determines whether the specified <see cref="DependencyObject"/> has the specified name.
        /// </summary>
        /// <param name="dependencyObject">The dependency object.</param>
        /// <param name="name">The name that the name of the <see cref="DependencyObject"/> should match.</param>
        /// <returns>
        /// 	<c>true</c> if the specified <see cref="DependencyObject"/> has the specified name; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException">when <paramref name="dependencyObject"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">when <paramref name="name"/> is <c>null</c> or empty.</exception>
        public static bool IsElementWithName(DependencyObject dependencyObject, string name)
        {
            if (dependencyObject == null)
            {
                throw new ArgumentNullException("dependencyObject");
            }

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(Exceptions.ArgumentCannotBeNullOrEmpty, "name");
            }

            if (dependencyObject is FrameworkElement)
            {
                return (string.Compare(((FrameworkElement) dependencyObject).Name, name, StringComparison.InvariantCulture) == 0);
            }

            return false;
        }
    }
}

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