diff --git a/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs b/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs new file mode 100644 index 0000000..d79cfea --- /dev/null +++ b/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs @@ -0,0 +1,109 @@ +using DeftSharp.Windows.Input.Interceptors; +using DeftSharp.Windows.Input.Mouse.Interceptors; + + +namespace DeftSharp.Windows.Input.Tests.CustomInterceptors; + +public sealed class CustomInterceptorTests +{ + private readonly MouseManipulator _mouseManipulator = new(); + + + [Fact] + public async void CustomInterceptor_CatchEvent() + { + ScrollDisabler _scrollDisable = new(); + MouseLogger _mouseLog = new(); + + _scrollDisable.Hook(); + _mouseLog.Hook(); + + + //Simulate a mouse click + _mouseManipulator.Click(); + + //Assert + var exception = await _mouseLog.ExceptionThrown.Task.WaitAsync(TimeSpan.FromSeconds(1)); + + Assert.NotNull(exception); + Assert.IsType(exception); + + + _scrollDisable.Unhook(); + _mouseLog.Unhook(); + + + } + + [Fact] + public async void CustomInterceptor_BlockEvent() + { + ScrollDisabler _scrollDisable = new(); + MouseLogger _mouseLog = new(); + + _scrollDisable.Hook(); + _mouseLog.Hook(); + + + //Simulate a mouse scroll + _mouseManipulator.Scroll(400); + + //Assert + var exception = await _mouseLog.ExceptionThrown.Task.WaitAsync(TimeSpan.FromSeconds(1)); + + Assert.NotNull(exception); + Assert.IsType(exception); + + + _scrollDisable.Unhook(); + _mouseLog.Unhook(); + + + } + + +} + +//Helper Custom Interceptor Classes +internal class ScrollDisabler : MouseInterceptor +{ + protected override bool IsInputAllowed(MouseInputArgs args) + { + if (args.Event is MouseInputEvent.Scroll) + return false; + + return true; + } +} + +internal class MouseLogger : MouseInterceptor +{ + internal TaskCompletionSource ExceptionThrown { get; } = new(); + + protected override bool IsInputAllowed(MouseInputArgs args) => true; + + protected override void OnInputSuccess(MouseInputArgs args) + { + ExceptionThrown.TrySetResult(new EventCatchedException("Click Catched")); + } + + protected override void OnInputFailure(MouseInputArgs args, IEnumerable failedInterceptors) + { + ExceptionThrown.TrySetResult(new EventBlockedException("Scroll Blocked")); + } +} + +public class EventCatchedException : Exception +{ + public EventCatchedException(string message) : base(message) + { + } +} + +public class EventBlockedException : Exception +{ + public EventBlockedException(string message) : base(message) + { + } +} + diff --git a/WPF.Playground/MainWindow.xaml.cs b/WPF.Playground/MainWindow.xaml.cs index b7f7a09..73384d1 100644 --- a/WPF.Playground/MainWindow.xaml.cs +++ b/WPF.Playground/MainWindow.xaml.cs @@ -1,7 +1,14 @@ -using System.Windows; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Threading; +using System.Windows; using System.Windows.Input; +using DeftSharp.Windows.Input.Extensions; +using DeftSharp.Windows.Input.Interceptors; using DeftSharp.Windows.Input.Keyboard; using DeftSharp.Windows.Input.Mouse; +using DeftSharp.Windows.Input.Mouse.Interceptors; namespace WPF.Playground { @@ -32,25 +39,37 @@ public partial class MainWindow public MainWindow() => InitializeComponent(); - + private ScrollDisabler sd = new ScrollDisabler(); + private MouseLogger mml = new MouseLogger(); private void OnLoaded(object sender, RoutedEventArgs e) { } private void OnClickButton1(object sender, RoutedEventArgs e) { + + sd.Hook(); + mml.Hook(); } private void OnClickButton2(object sender, RoutedEventArgs e) { + sd.Unhook(); + mml.Unhook(); } private void OnClickButton3(object sender, RoutedEventArgs e) { + Trace.WriteLine("Sleeping..."); + Thread.Sleep(3000); + _mouseManipulator.Click(); } private void OnClickButton4(object sender, RoutedEventArgs e) { + Trace.WriteLine("Sleeping..."); + Thread.Sleep(3000); + _mouseManipulator.Scroll(400); } private void OnClickButton5(object sender, RoutedEventArgs e)