WPF 自定义控件 - 如何对自定义控件进行单元测试?
基本上,我正在寻找有关如何对 WPF 自定义控件进行单元测试的资源/指南。
在这个特定的实例中,我创建的自定义控件恰好扩展了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;
}
}
PS 我正在使用内置的 Visual Studio 测试框架 (Microsoft.VisualStudio.QualityTools.UnitTestFramework
)。
为了避免因暴露内存中的明文密码而引起强烈反对:我知道我通过在 DependencyProperty 中暴露明文密码来违背 Microsoft 的安全推理,但考虑到我能够使用 Snoop 从标准密码框公开明文密码我发现它不再那么重要了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 UI 自动化,请参阅以下链接了解详细信息:
UI 自动化概述
WPF 自定义控件的 UI 自动化
You can use UI Automation, see the following links for more info:
UI Automation Overview
UI Automation of a WPF Custom Control