简体   繁体   English

WPF自定义控件 - 如何对自定义控件进行单元测试?

[英]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. 基本上,我正在寻找有关如何对WPF自定义控件进行单元测试的资源/指南。

In this particular instance, the custom control I created happens to extend the Decorator class. 在这个特定的实例中,我创建的自定义控件恰好扩展了Decorator类。 It wraps a PasswordBox child to expose the CLR Password Property as a DependencyProperty. 它包装一个PasswordBox子项以将CLR密码属性公开为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;
    }
}

PS I'm using the built-in Visual Studio Testing Framework ( Microsoft.VisualStudio.QualityTools.UnitTestFramework ). PS我正在使用内置的Visual Studio测试框架( 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. 为了避免在内存中暴露明文密码的反对:我明白我通过在DependencyProperty中公开明文密码来反对微软的安全推理,但考虑到我能够使用Snoop从标准PasswordBox中公开明文密码再也找不到那么重要了。

You can use UI Automation, see the following links for more info: 您可以使用UI Automation,有关详细信息,请参阅以下链接:

UI Automation Overview UI自动化概述

UI Automation of a WPF Custom Control WPF自定义控件的UI自动化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM