Skip to content

Commit 86fa845

Browse files
Add tests for EventSimulationExtensions
1 parent 3a2d11d commit 86fa845

File tree

4 files changed

+108
-6
lines changed

4 files changed

+108
-6
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
namespace SharpHook;
2+
3+
public sealed class EventSimulationExtensionsTests
4+
{
5+
[Property(DisplayName = "AddKeyStroke with an array should add events to the sequence")]
6+
public void AddKeyStrokeArray(KeyCode[] keyCodes)
7+
{
8+
// Arrange
9+
10+
var provider = new TestProvider();
11+
var builder = new EventSimulationSequenceBuilder(provider);
12+
13+
// Act
14+
15+
var result = builder
16+
.AddKeyStroke(keyCodes)
17+
.Simulate();
18+
19+
// Assert
20+
21+
AssertKeyStroke(keyCodes, provider);
22+
23+
Assert.Equal(UioHookResult.Success, result);
24+
}
25+
26+
[Property(DisplayName = "AddKeyStroke with an enumerable should add events to the sequence")]
27+
public void AddKeyStrokeEnumerable(List<KeyCode> keyCodes)
28+
{
29+
// Arrange
30+
31+
var provider = new TestProvider();
32+
var builder = new EventSimulationSequenceBuilder(provider);
33+
34+
// Act
35+
36+
var result = builder
37+
.AddKeyStroke(keyCodes)
38+
.Simulate();
39+
40+
// Assert
41+
42+
AssertKeyStroke(keyCodes, provider);
43+
44+
Assert.Equal(UioHookResult.Success, result);
45+
}
46+
47+
[Property(DisplayName = "SimulateKeyStroke with an array should simulate events")]
48+
public void SimulateKeyStrokeArray(KeyCode[] keyCodes)
49+
{
50+
// Arrange
51+
52+
var provider = new TestProvider();
53+
var simulator = new EventSimulator(provider);
54+
55+
// Act
56+
57+
var result = simulator.SimulateKeyStroke(keyCodes);
58+
59+
// Assert
60+
61+
AssertKeyStroke(keyCodes, provider);
62+
63+
Assert.Equal(UioHookResult.Success, result);
64+
}
65+
66+
[Property(DisplayName = "SimulateKeyStroke with an enumerable should simulate events")]
67+
public void SimulateKeyStrokeEnumerable(List<KeyCode> keyCodes)
68+
{
69+
// Arrange
70+
71+
var provider = new TestProvider();
72+
var simulator = new EventSimulator(provider);
73+
74+
// Act
75+
76+
var result = simulator.SimulateKeyStroke(keyCodes);
77+
78+
// Assert
79+
80+
AssertKeyStroke(keyCodes, provider);
81+
82+
Assert.Equal(UioHookResult.Success, result);
83+
}
84+
85+
private void AssertKeyStroke(IReadOnlyList<KeyCode> keyCodes, TestProvider provider)
86+
{
87+
Assert.Equal(keyCodes.Count * 2, provider.PostedEvents.Count);
88+
89+
foreach (var (keyCode, actualEvent) in keyCodes.Zip(provider.PostedEvents))
90+
{
91+
Assert.Equal(EventType.KeyPressed, actualEvent.Type);
92+
Assert.Equal(keyCode, actualEvent.Keyboard.KeyCode);
93+
}
94+
95+
foreach (var (keyCode, actualEvent) in
96+
Enumerable.Reverse(keyCodes).Zip(provider.PostedEvents.Skip(keyCodes.Count)))
97+
{
98+
Assert.Equal(EventType.KeyReleased, actualEvent.Type);
99+
Assert.Equal(keyCode, actualEvent.Keyboard.KeyCode);
100+
}
101+
}
102+
}

SharpHook/EventSimulationExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public IEventSimulationSequenceBuilder AddKeyStroke(params KeyCode[] keyCodes)
5353
/// <remarks>
5454
/// As an example, if the method is called with the following parameters:
5555
/// <code>
56-
/// builder.AddKeyStroke(KeyCode.VcLeftControl, KeyCode.VcC);
56+
/// builder.AddKeyStroke([KeyCode.VcLeftControl, KeyCode.VcC]);
5757
/// </code>
5858
/// then this will be equivalent to calling the following method sequence:
5959
/// <code>
@@ -107,7 +107,7 @@ public UioHookResult SimulateKeyStroke(params KeyCode[] keyCodes) =>
107107
/// <remarks>
108108
/// As an example, if the method is called with the following parameters:
109109
/// <code>
110-
/// simulator.SimulateKeyStroke(KeyCode.VcLeftControl, KeyCode.VcC);
110+
/// simulator.SimulateKeyStroke([KeyCode.VcLeftControl, KeyCode.VcC]);
111111
/// </code>
112112
/// then this will be equivalent to calling the following method sequence:
113113
/// <code>

SharpHook/SharpHook.xml

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

SharpHook/Testing/TestProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public TestProvider(TestThreadingMode threadingMode) =>
5555
public TestThreadingMode ThreadingMode { get; }
5656

5757
/// <summary>
58-
/// Gets the events that have been posted using <see cref="PostEvent(ref UioHookEvent)" />.
58+
/// Gets the events that have been posted using <see cref="PostEvent" /> or <see cref="PostEvents" />.
5959
/// </summary>
6060
public IReadOnlyList<UioHookEvent> PostedEvents =>
6161
this.postedEvents.AsReadOnly();

0 commit comments

Comments
 (0)