Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 虚拟键盘测试是';我不能用我的键盘楔子_C#_Winforms_Nunit - Fatal编程技术网

C# 虚拟键盘测试是';我不能用我的键盘楔子

C# 虚拟键盘测试是';我不能用我的键盘楔子,c#,winforms,nunit,C#,Winforms,Nunit,我已经安装了com0com,以便编写NUnit测试。以防万一,键盘楔块的定义有所不同。对它的简要描述是一种软件,它监听串行通信设备,读取发送给它的任何数据(在我的例子中,将其格式化为ASCII数据),然后将其发送到虚拟键盘。这段代码在生产环境中确实有效,但是我们现在需要记录我们的代码或者进行单元测试来证明它应该如何使用。这是我的测试 [Test()] public void WedgeSendsTextToVirtualKeyboardTest() { (

我已经安装了com0com,以便编写NUnit测试。以防万一,键盘楔块的定义有所不同。对它的简要描述是一种软件,它监听串行通信设备,读取发送给它的任何数据(在我的例子中,将其格式化为ASCII数据),然后将其发送到虚拟键盘。这段代码在生产环境中确实有效,但是我们现在需要记录我们的代码或者进行单元测试来证明它应该如何使用。这是我的测试

    [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是string.Empty。我尝试了许多不同的策略来关注这个文本框(我想这至少是个问题)。有一段时间,我让我的睡眠时间更长,这样我就有时间点击文本框并键入自己的内容,但我无法点击该框(我假设这是因为睡眠操作…这也可能是我的楔子无法在其中键入内容的原因),所以我如何解决这个问题并通过测试。同样,这段代码在生产环境中也能工作,所以我100%相信这是我的测试(可能是睡眠操作)

我能够通过这个问题(非常感谢Patrick Quirk)。它实际上是它的一个小变化。我甚至不确定我的解决方案是否100%正确,但当表单弹出时,输入文本,我的测试通过。解决方案是由两部分组成的系统。首先,我必须创建一个扩展
Form
覆盖
Text
属性的类,并侦听
激活的
FormClosing
事件。在表单关闭时,我会设置文本,在激活时,我告诉我的
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;
    }
[Test()]
public void WedgeSendsTextToVirtualKeyboardTest()
{
使用(var form=new WedgeForm())
使用(var楔形=新键盘楔形配置(楔形键))
{
旋转键盘楔块(楔块);
string-actual=MakeWedgeWriteHelloWorld(form,wedge);
字符串应为=“Hello World”;
预期(实际,等于预期);
}
}
私有静态字符串MakeWedgeWriteHelloWorld(楔形窗体、键盘楔形配置楔形)
{
var uiThread=新线程(()=>Application.Run(form));
SetApartmentState(ApartmentState.STA);
uiThread.Start();
string实际值=string.Empty;
var线程=新线程
(
()=>actual=InvokeEx(form,f=>f.Text)
);
使用(var sp=new System.IO.Ports.SerialPort(“COM”+COMB,115200))
{
sp.Open();
sp.Write(“你好世界”);
}
//等待1秒。这将允许发送数据,并将数据汇集到楔块中
//最短等待时间为200ms。然后将字符串放入字节
//然后被传送到一个虚拟键盘上,在那里所有的键都被按下。
睡眠(1000);
InvokeEx(form,f=>f.Close());
thread.Start();
uiThread.Join();
thread.Join();
返回实际值;
}
运行此测试时,我必须记住的一件小事是不要到处点击,因为如果文本框失去焦点,我就会沉沦。但是测试只有1秒长。。我想我会活下去的。

我想我可以帮你指引方向。您可以做类似的事情,将表单移动到它自己的线程,也可以将串行端口写入另一个线程,然后等待它们。