虚拟键盘测试不能与我的键盘楔子一起工作
本文关键字:键盘 一起 工作 测试 不能 虚拟 我的 | 更新日期: 2023-09-27 18:05:49
我已经安装了com0com,这样我就可以编写一个NUnit测试。以防键盘楔子的定义不同,它的简要描述是一个软件,监听串行通信设备,读取发送给它的任何数据(在我的情况下将其格式为ASCII数据),然后将其发送到虚拟键盘。这些代码在生产环境中确实可以工作,但我们现在需要为代码编写文档,或者进行单元测试,以证明应该如何使用这些代码。这里是test
[Test()]
public void WedgeSendsTextToVirtualKeyboardTest()
{
(var form = new Form())
using(var sp = new System.IO.Ports.SerialPort("COM"+COMB, 115200))
using (var wedge = new KeyboardWedgeConfiguration(WEDGE_KEY))
{
sp.Open();
TextBox tb = SetupForm(form);
TurnOnKeyboardWedge(wedge);
form.Activate();
form.Activated += (s, e) =>
{
tb.Focus();
};
while (!tb.Focused) { }
string str = "Hello World";
sp.Write(str);
//wait 1 second. This allows data to send, and pool in the wedge
//the minimum wait time is 200ms. the string then gets put into bytes
//and shipped off to a virtual keyboard where all the keys are pressed.
System.Threading.Thread.Sleep(1000);
Expect(tb.Text, Is.EqualTo(str));
}
}
private static TextBox SetupForm(Form form)
{
TextBox tb = new TextBox();
tb.Name = "tb";
tb.TabIndex = 0;
tb.AcceptsReturn = true;
tb.AcceptsTab = true;
tb.Dock = DockStyle.Fill;
form.Controls.Add(tb);
form.Show();
return tb;
}
private static void TurnOnKeyboardWedge(KeyboardWedgeConfiguration wedge)
{
wedge.Port = COMA;
wedge.PortForwardingEnabled = true;
wedge.Baud = 115200;
System.IO.Ports.SerialPort serialPort;
wedge.StartRerouting();
Assert.IsTrue(wedge.IsAlive(out serialPort));
Assert.IsNotNull(serialPort);
}
当测试运行时,表单显示,没有文本放入文本框中,然后测试退出,最后一个断言失败(Expect(tb.Text, Is.EqualTo(str));
)说tb。Text是字符串,空。我尝试了许多不同的策略来关注文本框(我认为这至少是问题所在)。有一次,我延长了睡眠时间,这样我就有时间点击文本框并自己打字了,但我无法点击文本框(我猜这是因为睡眠操作……这也可能是为什么我的楔子不能在那里键入,以及)所以我怎么能解决这个问题,使我的测试通过。同样,这段代码在生产环境中工作,所以我100%确信这是我的测试(可能是睡眠操作)
在这个问题stackoverflow问题的帮助下,我能够使我的测试通过(非常感谢Patrick Quirk)。它实际上是它的一个小变化。我甚至不确定我的解决方案是否100%正确,但当表单弹出文本输入和我的测试通过。解决方案是两部分系统。首先,我必须创建一个扩展Form
覆盖Text
属性的类,并侦听Activated
和FormClosing
事件。在Form Closing选项中,我设置文本,在Activated选项中,我让TextBox
设置焦点。
private class WedgeForm : Form
{
public override string Text { get { return text; } set { text = value; } }
string text = string.Empty;
private TextBox tb;
public WedgeForm()
{
InitializeControls();
Activated += (s, e) => { tb.Focus(); };
FormClosing += (s, e) => { this.Text = tb.Text; };
}
private void InitializeControls()
{
tb = new TextBox();
tb.Name = "tb";
tb.TabIndex = 0;
tb.AcceptsReturn = true;
tb.AcceptsTab = true;
tb.Multiline = true;
tb.Dock = DockStyle.Fill;
this.Controls.Add(tb);
}
}
然后使用另一个问题/答案中提供的InvokeEx方法我的测试很容易设置
[Test()]
public void WedgeSendsTextToVirtualKeyboardTest()
{
using (var form = new WedgeForm())
using (var wedge = new KeyboardWedgeConfiguration(WEDGE_KEY))
{
TurnOnKeyboardWedge(wedge);
string actual = MakeWedgeWriteHelloWorld(form, wedge); ;
string expected = "Hello World";
Expect(actual, Is.EqualTo(expected));
}
}
private static string MakeWedgeWriteHelloWorld(WedgeForm form, KeyboardWedgeConfiguration wedge)
{
var uiThread = new Thread(() => Application.Run(form));
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.Start();
string actual = string.Empty;
var thread = new Thread
(
() => actual = InvokeEx<Form, string>(form, f => f.Text)
);
using (var sp = new System.IO.Ports.SerialPort("COM" + COMB, 115200))
{
sp.Open();
sp.Write("Hello World");
}
//wait 1 second. This allows data to send, and pool in the wedge
//the minimum wait time is 200ms. the string then gets put into bytes
//and shipped off to a virtual keyboard where all the keys are pressed.
Thread.Sleep(1000);
InvokeEx<Form>(form, f => f.Close());
thread.Start();
uiThread.Join();
thread.Join();
return actual;
}
在运行这个测试时,我必须记住的一件小事是不要到处点击,因为如果文本框失去焦点,我就会沉没。但是测试只有1秒长…我想我会活下去的。