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

时间:2022-09-02 13:32:53

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. It wraps a PasswordBox child to expose the CLR Password Property as a DependencyProperty.

在这个特定的实例中,我创建的自定义控件碰巧扩展了Decorator类。它封装一个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;
    }
}

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

我正在使用内置的VisualStudio测试框架(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的明文密码我不觉得那么重要了。

1 个解决方案

#1


3  

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

您可以使用UI自动化,更多信息请参见以下链接:

UI Automation Overview

UI自动化概述

UI Automation of a WPF Custom Control

WPF自定义控件的UI自动化

#1


3  

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

您可以使用UI自动化,更多信息请参见以下链接:

UI Automation Overview

UI自动化概述

UI Automation of a WPF Custom Control

WPF自定义控件的UI自动化