2

私はいくつかの小さな問題を抱えています。Visual Studio 2010 で c#/wpf/interactivty プロジェクトの単体テストを書きたいと思っています。

unittest は、テキスト ボックスで (仮想) キー ダウン イベントをシミュレートする必要があり、結果はアクションを発生させる必要があります (アクションの結果: コンソール出力 - 最初のステップとして確認するためだけに)

私はまだ2つの問題を修正しました->ディスパッチャーの問題とpresentationSourceのバグ。

単体テストはまだキーイベントをシミュレートし、キーイベントはテキストボックスに到達しましたが、問題は、なぜテキストボックスのキーダウンイベントによってアクションが発生しないのですか?

それはスレッドの問題ですか?私の誤解は何ですか?

ここにコードがあります

ユニットテスト
の最後のユニットテストでは、テキストボックスを確認できます-キーボードは機能します

[TestMethod]
public void simpleTest()
{
    var mockWindow = new MockWindow();

    //simple test to check if the virtualKeyboard works
    string CheckText = "Checktext";

    mockWindow.SendToUIThread(mockWindow.textbox, CheckText);

    mockWindow.SendToUIThread(mockWindow.textbox, "k");

    //needed to start the dispatcher
    DispatcherUtil.DoEvents();
}

ディスパッチャの修正

public static class DispatcherUtil
    {
        [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        public static void DoEvents()
        {
            DispatcherFrame frame = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame);
            Dispatcher.PushFrame(frame);
        }

        private static object ExitFrame(object frame)
        {
            ((DispatcherFrame)frame).Continue = false;
            return null;
        }
    }

私の証

 class TestAction : TriggerAction<UIElement>
    {
        protected override void Invoke(object parameter)
        {
            Console.WriteLine("testAction invoke");
        }
    }

モックウィンドウ

public class MockWindow : Window
{
    public TextBox textbox { get; private set; }

    public MockWindow()
    {    
        //add a grid&textbox
        Grid grid = new Grid();
        textbox = new TextBox();

        this.Content = grid;
        grid.Children.Add(textbox);

        //create the testaction/triggerEvent & add them
        TestAction testAction = new TestAction();
        System.Windows.Interactivity.EventTrigger TestTrigger = new System.Windows.Interactivity.EventTrigger();
        TestTrigger.EventName = "KeyDown";
        TestTrigger.Actions.Add(testAction);
        TestTrigger.Attach(this.textbox);
    }

    //enter a keyboard press on an UIElement
    public void SendToUIThread(UIElement element, string text)
    {
        element.Dispatcher.BeginInvoke(new Action(() =>
        {
            SendKeys.Send(element, text);
        }), DispatcherPriority.Input);
    }
}

codeplex sendkeys から追加されたMockKeyboard + unittest の presentationCore 修正 (クラス SendKeys で追加)

public class FixPresentationSource : PresentationSource
{
    protected override CompositionTarget GetCompositionTargetCore()
    {
        return null;
    }

    public override Visual RootVisual { get; set; }

    public override bool IsDisposed { get { return false; } }
}
4

0 に答える 0