Skip to content

Commit 4bc7cdb

Browse files
Add tests for TestProvider.PostEvents
1 parent 82a7502 commit 4bc7cdb

File tree

5 files changed

+189
-25
lines changed

5 files changed

+189
-25
lines changed

SharpHook.Tests/Testing/TestProviderTests.cs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ namespace SharpHook.Testing;
22

33
public sealed class TestProviderTests
44
{
5+
private static readonly Random random = new();
6+
57
[Property(DisplayName = "SetDispatchProc, Run, and PostEvent should work together")]
68
public void Run(UioHookEvent eventToPost, IntPtr userData)
79
{
@@ -47,7 +49,7 @@ public void Run(UioHookEvent eventToPost, IntPtr userData)
4749
provider.Stop();
4850
}
4951

50-
[Property(DisplayName = "RunAsync, SetDispatchProc, and PostEvent should work together")]
52+
[Property(DisplayName = "SetDispatchProc, RunAsync, and PostEvent should work together")]
5153
public async Task RunAsync(UioHookEvent eventToPost, nint userData)
5254
{
5355
// Arrange
@@ -514,6 +516,84 @@ public void PostEventFail(UioHookEvent eventToPost, FailedUioHookResult result)
514516
Assert.Equal(result.Value, actualResult);
515517
}
516518

519+
[Property(DisplayName = "PostEvents should work the same as multiple PostEvent calls")]
520+
public void PostEvents(NonEmptyArray<UioHookEvent> events)
521+
{
522+
// Arrange
523+
524+
var eventsToPost = events.Get;
525+
int size = random.Next(eventsToPost.Length);
526+
527+
var provider = new TestProvider();
528+
529+
// Act
530+
531+
var result = provider.PostEvents(eventsToPost, (uint)size);
532+
533+
// Assert
534+
535+
Assert.Equal(UioHookResult.Success, result);
536+
Assert.Equal(size, provider.PostedEvents.Count);
537+
538+
foreach (var (expected, actual) in eventsToPost.Zip(provider.PostedEvents))
539+
{
540+
Assert.Equal(expected, actual);
541+
}
542+
}
543+
544+
[Property(DisplayName = "PostEvents should return an error result if configured to do so")]
545+
public void PostEventsError(NonEmptyArray<UioHookEvent> events, FailedUioHookResult result)
546+
{
547+
// Arrange
548+
549+
var eventsToPost = events.Get;
550+
551+
var provider = new TestProvider
552+
{
553+
PostEventResult = result.Value
554+
};
555+
556+
// Act
557+
558+
var actualResult = provider.PostEvents(eventsToPost, (uint)eventsToPost.Length);
559+
560+
// Assert
561+
562+
Assert.Equal(actualResult, result.Value);
563+
Assert.Empty(provider.PostedEvents);
564+
}
565+
566+
[Property(DisplayName = "PostEvents should return an error result when the events array is null")]
567+
public void PostEventsNull()
568+
{
569+
// Arrange
570+
571+
var provider = new TestProvider();
572+
573+
// Act
574+
575+
var result = provider.PostEvents(null!, 1);
576+
577+
// Assert
578+
579+
Assert.Equal(UioHookResult.ErrorNull, result);
580+
}
581+
582+
[Property(DisplayName = "PostEvents should throw when the size is larger than the length of the array")]
583+
public void PostEventsInvalidSize(NonEmptyArray<UioHookEvent> events)
584+
{
585+
// Arrange
586+
587+
var eventsToPost = events.Get;
588+
int size = random.Next(eventsToPost.Length) + eventsToPost.Length + 1;
589+
590+
var provider = new TestProvider();
591+
592+
// Act + Assert
593+
594+
Assert.Throws<ArgumentOutOfRangeException>(() => provider.PostEvents(eventsToPost, (uint)size));
595+
}
596+
517597
[Property(DisplayName = "PostText should post text")]
518598
public void PostText(NonNull<string> text)
519599
{

SharpHook.Tests/Testing/TestProviderWithEventLoopTests.cs

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ namespace SharpHook.Testing;
22

33
public sealed class TestProviderWithEventLoopTests
44
{
5-
[Property(DisplayName = "Run, SetDispatchProc, and PostEvent should work together")]
5+
private static readonly Random random = new();
6+
7+
[Property(DisplayName = "SetDispatchProc, Run, and PostEvent should work together")]
68
public void Run(UioHookEvent eventToPost, nint userData)
79
{
810
// Arrange
@@ -45,7 +47,7 @@ public void Run(UioHookEvent eventToPost, nint userData)
4547
Assert.Equal(eventToPost, provider.PostedEvents[0]);
4648
}
4749

48-
[Property(DisplayName = "RunAsync, SetDispatchProc, and PostEvent should work together")]
50+
[Property(DisplayName = "SetDispatchProc, RunAsync, and PostEvent should work together")]
4951
public async Task RunAsync(UioHookEvent eventToPost, nint userData)
5052
{
5153
// Arrange
@@ -495,6 +497,92 @@ public void PostEventFail(UioHookEvent eventToPost, FailedUioHookResult result)
495497
Assert.Equal(result.Value, actualResult);
496498
}
497499

500+
[Property(DisplayName = "PostEvents should work the same as multiple PostEvent calls")]
501+
public void PostEvents(NonEmptyArray<UioHookEvent> events)
502+
{
503+
// Arrange
504+
505+
var eventsToPost = events.Get;
506+
int size = random.Next(eventsToPost.Length);
507+
508+
var provider = new TestProvider(TestThreadingMode.EventLoop);
509+
510+
// Act
511+
512+
this.RunAndWaitForStart(provider);
513+
514+
var result = provider.PostEvents(eventsToPost, (uint)size);
515+
516+
this.StopAndWaitForStop(provider);
517+
518+
// Assert
519+
520+
Assert.Equal(UioHookResult.Success, result);
521+
Assert.Equal(size, provider.PostedEvents.Count);
522+
523+
foreach (var (expected, actual) in eventsToPost.Zip(provider.PostedEvents))
524+
{
525+
Assert.Equal(expected, actual);
526+
}
527+
}
528+
529+
[Property(DisplayName = "PostEvents should return an error result if configured to do so")]
530+
public void PostEventsError(NonEmptyArray<UioHookEvent> events, FailedUioHookResult result)
531+
{
532+
// Arrange
533+
534+
var eventsToPost = events.Get;
535+
536+
var provider = new TestProvider(TestThreadingMode.EventLoop)
537+
{
538+
PostEventResult = result.Value
539+
};
540+
541+
// Act
542+
543+
this.RunAndWaitForStart(provider);
544+
545+
var actualResult = provider.PostEvents(eventsToPost, (uint)eventsToPost.Length);
546+
547+
this.StopAndWaitForStop(provider);
548+
549+
// Assert
550+
551+
Assert.Equal(actualResult, result.Value);
552+
Assert.Empty(provider.PostedEvents);
553+
}
554+
555+
[Property(DisplayName = "PostEvents should return an error result when the events array is null")]
556+
public void PostEventsNull()
557+
{
558+
// Arrange
559+
560+
var provider = new TestProvider(TestThreadingMode.EventLoop);
561+
562+
// Act
563+
564+
var result = provider.PostEvents(null!, 1);
565+
566+
// Assert
567+
568+
Assert.Equal(UioHookResult.ErrorNull, result);
569+
}
570+
571+
[Property(DisplayName = "PostEvents should throw when the size is larger than the length of the array")]
572+
public void PostEventsInvalidSize(NonEmptyArray<UioHookEvent> events)
573+
{
574+
// Arrange
575+
576+
var eventsToPost = events.Get;
577+
int size = random.Next(eventsToPost.Length) + eventsToPost.Length + 1;
578+
579+
var provider = new TestProvider(TestThreadingMode.EventLoop);
580+
581+
// Act + Assert
582+
583+
Assert.Throws<ArgumentOutOfRangeException>(() => provider.PostEvents(eventsToPost, (uint)size));
584+
}
585+
498586
[Property(DisplayName = "PostText should post text")]
499587
public void PostText(NonNull<string> text)
500588
{

SharpHook/EventSimulationExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public static class EventSimulationExtensions
2525
/// .AddKeyRelease(KeyCode.VcC)
2626
/// .AddKeyRelease(KeyCode.VcLeftControl);
2727
/// </code>
28-
/// which means that this method will add pressing the left control, then pressing C, then releasing C, then
29-
/// releasing the left control.
28+
/// which means that this method will add pressing Left Control, then pressing C, then releasing C, then releasing
29+
/// Left Control.
3030
/// </remarks>
3131
public static IEventSimulationSequenceBuilder AddKeyStroke(
3232
this IEventSimulationSequenceBuilder builder,
@@ -65,8 +65,8 @@ public static IEventSimulationSequenceBuilder AddKeyStroke(
6565
/// .AddKeyRelease(KeyCode.VcC)
6666
/// .AddKeyRelease(KeyCode.VcLeftControl);
6767
/// </code>
68-
/// which means that this method will add pressing the left control, then pressing C, then releasing C, then
69-
/// releasing the left control.
68+
/// which means that this method will add pressing Left Control, then pressing C, then releasing C, then releasing
69+
/// Left Control.
7070
/// </remarks>
7171
public static IEventSimulationSequenceBuilder AddKeyStroke(
7272
this IEventSimulationSequenceBuilder builder,
@@ -93,8 +93,8 @@ public static IEventSimulationSequenceBuilder AddKeyStroke(
9393
/// .AddKeyRelease(KeyCode.VcLeftControl)
9494
/// .Simulate();
9595
/// </code>
96-
/// which means that this method will simualte pressing the left control, then pressing C, then releasing C, then
97-
/// releasing the left control.
96+
/// which means that this method will simualte pressing Left Control, then pressing C, then releasing C, then
97+
/// releasing Left Control.
9898
/// </remarks>
9999
public static UioHookResult SimulateKeyStroke(this IEventSimulator simulator, params KeyCode[] keyCodes) =>
100100
simulator.Sequence()
@@ -121,8 +121,8 @@ public static UioHookResult SimulateKeyStroke(this IEventSimulator simulator, pa
121121
/// .AddKeyRelease(KeyCode.VcLeftControl)
122122
/// .Simulate();
123123
/// </code>
124-
/// which means that this method will simualte pressing the left control, then pressing C, then releasing C, then
125-
/// releasing the left control.
124+
/// which means that this method will simualte pressing Left Control, then pressing C, then releasing C, then
125+
/// releasing Left Control.
126126
/// </remarks>
127127
public static UioHookResult SimulateKeyStroke(this IEventSimulator simulator, IEnumerable<KeyCode> keyCodes) =>
128128
simulator.Sequence()

SharpHook/EventSimulationSequenceTemplate.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public sealed class EventSimulationSequenceTemplate : IEventSimulationSequenceTe
1515
/// <param name="simulationProvider">
1616
/// The simulation functionality provider (or <see cref="UioHookProvider.Instance" /> if <see langword="null" />).
1717
/// </param>
18-
/// <exception cref="ArgumentNullException">
19-
/// <paramref name="events" /> is <see langword="null" />.
20-
/// </exception>
18+
/// <exception cref="ArgumentNullException"><paramref name="events" /> is <see langword="null" />.</exception>
2119
[SuppressMessage(
2220
"Style", "IDE0290:Use primary constructor", Justification = "Primary constructors don't support XML comments")]
2321
public EventSimulationSequenceTemplate(

SharpHook/SharpHook.xml

Lines changed: 9 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)