Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Custom Control - How do you unit test a custom control?

Basically, I'm looking for resources/guides on how to unit test a WPF Custom Control.

In this particular instance, the custom control I created happens to extend the Decorator class. It wraps a PasswordBox child to expose the CLR Password Property as a DependencyProperty.

public class BindablePasswordBox : Decorator
{
    public BindablePasswordBox()
    {
        Child = new PasswordBox();
        ((PasswordBox)Child).PasswordChanged += this.PasswordChanged;
    }

    public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.Register("Password", typeof(String), typeof(BindablePasswordBox),
            new FrameworkPropertyMetadata
            {
                BindsTwoWayByDefault = true,
                DefaultUpdateSourceTrigger = UpdateSourceTrigger.LostFocus
            });

    public String Password
    {
        get { return (String)GetValue(PasswordProperty); }
        set { SetValue(PasswordProperty, value); }
    }

    void PasswordChanged(Object sender, RoutedEventArgs e)
    {
        Password = ((PasswordBox)Child).Password;
    }
}

P.S. I'm using the built-in Visual Studio Testing Framework (Microsoft.VisualStudio.QualityTools.UnitTestFramework).


To avoid getting backlash about exposing plaintext passwords in memory: I understand that I'm going against Microsoft's security reasoning by exposing the plaintext password in a DependencyProperty, but considering that I was able to use Snoop to expose the plaintext password from a standard PasswordBox I don't find it all that important anymore.

like image 700
myermian Avatar asked Apr 01 '11 16:04

myermian


1 Answers

You can use UI Automation, see the following links for more info:

UI Automation Overview

UI Automation of a WPF Custom Control

like image 130
Mohammed A. Fadil Avatar answered Nov 03 '22 02:11

Mohammed A. Fadil