Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<EventCatchedException>(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<EventBlockedException>(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<Exception> 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<InterceptorInfo> 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)
{
}
}

23 changes: 21 additions & 2 deletions WPF.Playground/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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)
Expand Down