using System.Windows.Forms; using Azimuth; using Blacklight.Core.Cameras; using UnitTestSharp; namespace Blacklight.Core.UnitTests { public class WinFormsInputTests : TestFixture { public class TestControl : Control { public void RaiseMouseDown(MouseEventArgs e) { OnMouseDown(e); } public void RaiseMouseMove(MouseEventArgs e) { OnMouseMove(e); } public void RaiseMouseUp(MouseEventArgs e) { OnMouseUp(e); } public void RaiseMouseWheel(MouseEventArgs e) { OnMouseWheel(e); } public void RaiseKeyDown(KeyEventArgs e) { OnKeyDown(e); } public void RaiseKeyUp(KeyEventArgs e) { OnKeyUp(e); } } public class RecordingConsumer : IInputConsumer { public PointerInput? LastPointerDown; public PointerInput? LastPointerMove; public PointerInput? LastPointerUp; public int? LastWheelTicks; public Vector? LastWheelPixelPos; public Key? LastKeyDown; public Key? LastKeyUp; public void PointerDown(PointerInput input) { LastPointerDown = input; } public void PointerMove(PointerInput input) { LastPointerMove = input; } public void PointerUp(PointerInput input) { LastPointerUp = input; } public void Wheel(int ticks, Vector pixelPos) { LastWheelTicks = ticks; LastWheelPixelPos = pixelPos; } public void KeyDown(Key key) { LastKeyDown = key; } public void KeyUp(Key key) { LastKeyUp = key; } } public static (TestControl, RecordingConsumer, WinFormsInput) MakeInput() { var control = new TestControl(); var consumer = new RecordingConsumer(); var input = new WinFormsInput(control, consumer); return (control, consumer, input); } public class MouseDownTests : TestFixture { public void TranslatesPositionAndButtonToPointerDown() { var (control, consumer, _) = MakeInput(); control.RaiseMouseDown(new MouseEventArgs(MouseButtons.Left, 1, 12, 34, 0)); CheckEqual(new Vector(12, 34), consumer.LastPointerDown.Value.PixelPos); CheckEqual(PointerButtons.Left, consumer.LastPointerDown.Value.Buttons); } public void RightButtonTranslatesToRightPointerButton() { var (control, consumer, _) = MakeInput(); control.RaiseMouseDown(new MouseEventArgs(MouseButtons.Right, 1, 0, 0, 0)); CheckEqual(PointerButtons.Right, consumer.LastPointerDown.Value.Buttons); } } public class MouseMoveTests : TestFixture { public void TranslatesPositionAndHeldButtonToPointerMove() { var (control, consumer, _) = MakeInput(); control.RaiseMouseMove(new MouseEventArgs(MouseButtons.Left, 0, 30, 25, 0)); CheckEqual(new Vector(30, 25), consumer.LastPointerMove.Value.PixelPos); CheckEqual(PointerButtons.Left, consumer.LastPointerMove.Value.Buttons); } } public class MouseUpTests : TestFixture { public void TranslatesPositionAndButtonToPointerUp() { var (control, consumer, _) = MakeInput(); control.RaiseMouseUp(new MouseEventArgs(MouseButtons.Left, 1, 5, 6, 0)); CheckEqual(new Vector(5, 6), consumer.LastPointerUp.Value.PixelPos); CheckEqual(PointerButtons.Left, consumer.LastPointerUp.Value.Buttons); } } public class MouseWheelTests : TestFixture { public void PassesDeltaThroughWithoutSignFlip() { var (control, consumer, _) = MakeInput(); control.RaiseMouseWheel(new MouseEventArgs(MouseButtons.None, 0, 7, 8, 120)); CheckEqual(120, consumer.LastWheelTicks.Value); CheckEqual(new Vector(7, 8), consumer.LastWheelPixelPos.Value); } } public class KeyDownTests : TestFixture { public static readonly (Keys, Key)[] MappedKeys = { (Keys.A, Key.A), (Keys.B, Key.B), (Keys.C, Key.C), (Keys.D, Key.D), (Keys.E, Key.E), (Keys.F, Key.F), (Keys.G, Key.G), (Keys.H, Key.H), (Keys.I, Key.I), (Keys.J, Key.J), (Keys.K, Key.K), (Keys.L, Key.L), (Keys.M, Key.M), (Keys.N, Key.N), (Keys.O, Key.O), (Keys.P, Key.P), (Keys.Q, Key.Q), (Keys.R, Key.R), (Keys.S, Key.S), (Keys.T, Key.T), (Keys.U, Key.U), (Keys.V, Key.V), (Keys.W, Key.W), (Keys.X, Key.X), (Keys.Y, Key.Y), (Keys.Z, Key.Z), (Keys.D0, Key.Digit0), (Keys.D1, Key.Digit1), (Keys.D2, Key.Digit2), (Keys.D3, Key.Digit3), (Keys.D4, Key.Digit4), (Keys.D5, Key.Digit5), (Keys.D6, Key.Digit6), (Keys.D7, Key.Digit7), (Keys.D8, Key.Digit8), (Keys.D9, Key.Digit9), (Keys.F1, Key.F1), (Keys.F2, Key.F2), (Keys.F3, Key.F3), (Keys.F4, Key.F4), (Keys.F5, Key.F5), (Keys.F6, Key.F6), (Keys.F7, Key.F7), (Keys.F8, Key.F8), (Keys.F9, Key.F9), (Keys.F10, Key.F10), (Keys.F11, Key.F11), (Keys.F12, Key.F12), (Keys.Up, Key.ArrowUp), (Keys.Down, Key.ArrowDown), (Keys.Left, Key.ArrowLeft), (Keys.Right, Key.ArrowRight), (Keys.Home, Key.Home), (Keys.End, Key.End), (Keys.PageUp, Key.PageUp), (Keys.PageDown, Key.PageDown), (Keys.Insert, Key.Insert), (Keys.Delete, Key.Delete), (Keys.Space, Key.Space), (Keys.Enter, Key.Enter), (Keys.Escape, Key.Escape), (Keys.Tab, Key.Tab), (Keys.Back, Key.Backspace), (Keys.CapsLock, Key.CapsLock), (Keys.LShiftKey, Key.ShiftLeft), (Keys.RShiftKey, Key.ShiftRight), (Keys.LControlKey, Key.ControlLeft), (Keys.RControlKey, Key.ControlRight), (Keys.LMenu, Key.AltLeft), (Keys.RMenu, Key.AltRight), (Keys.OemMinus, Key.Minus), (Keys.Oemplus, Key.Equal), (Keys.OemOpenBrackets, Key.BracketLeft), (Keys.OemCloseBrackets, Key.BracketRight), (Keys.OemPipe, Key.Backslash), (Keys.OemSemicolon, Key.Semicolon), (Keys.OemQuotes, Key.Quote), (Keys.Oemcomma, Key.Comma), (Keys.OemPeriod, Key.Period), (Keys.OemQuestion, Key.Slash), (Keys.Oemtilde, Key.Backquote), }; public void TranslatesEveryMappedKeyToItsPortableEquivalent() { foreach (var (winFormsKey, expected) in MappedKeys) { var (control, consumer, _) = MakeInput(); control.RaiseKeyDown(new KeyEventArgs(winFormsKey)); CheckEqual(expected, consumer.LastKeyDown.Value); } } public void UnmappedKeyDoesNotReachConsumer() { var (control, consumer, _) = MakeInput(); control.RaiseKeyDown(new KeyEventArgs(Keys.NumLock)); CheckNull(consumer.LastKeyDown); } } public class KeyUpTests : TestFixture { public void MappedKeyTranslatesToItsPortableEquivalent() { var (control, consumer, _) = MakeInput(); control.RaiseKeyUp(new KeyEventArgs(Keys.PageUp)); CheckEqual(Key.PageUp, consumer.LastKeyUp.Value); } public void UnmappedKeyDoesNotReachConsumer() { var (control, consumer, _) = MakeInput(); control.RaiseKeyUp(new KeyEventArgs(Keys.NumLock)); CheckNull(consumer.LastKeyUp); } } public class DisposeTests : TestFixture { public void UnwiresAllEvents() { var (control, consumer, input) = MakeInput(); input.Dispose(); control.RaiseMouseDown(new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0)); control.RaiseMouseMove(new MouseEventArgs(MouseButtons.Left, 0, 0, 0, 0)); control.RaiseMouseUp(new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0)); control.RaiseMouseWheel(new MouseEventArgs(MouseButtons.None, 0, 0, 0, 120)); control.RaiseKeyDown(new KeyEventArgs(Keys.PageUp)); control.RaiseKeyUp(new KeyEventArgs(Keys.PageUp)); CheckNull(consumer.LastPointerDown); CheckNull(consumer.LastPointerMove); CheckNull(consumer.LastPointerUp); CheckNull(consumer.LastWheelTicks); CheckNull(consumer.LastKeyDown); CheckNull(consumer.LastKeyUp); } } } }