Skip to content

Commit 55afa4f

Browse files
committed
Source input actions as interface
1 parent a3aa07e commit 55afa4f

File tree

7 files changed

+51
-64
lines changed

7 files changed

+51
-64
lines changed
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
using OpenQA.Selenium.BiDi.Modules.Input;
22
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
35
using System.Text.Json;
46
using System.Text.Json.Serialization;
57

68
#nullable enable
79

810
namespace OpenQA.Selenium.BiDi.Communication.Json.Converters.Enumerable;
911

10-
internal class InputSourceActionsConverter : JsonConverter<SourceActions>
12+
internal class InputSourceActionsConverter : JsonConverter<ISourceActions>
1113
{
12-
public override SourceActions Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
14+
public override ISourceActions Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1315
{
1416
throw new NotImplementedException();
1517
}
1618

17-
public override void Write(Utf8JsonWriter writer, SourceActions value, JsonSerializerOptions options)
19+
public override void Write(Utf8JsonWriter writer, ISourceActions value, JsonSerializerOptions options)
1820
{
1921
writer.WriteStartObject();
2022

2123
writer.WriteString("id", value.Id);
2224

2325
switch (value)
2426
{
25-
case SourceActions.Keys keys:
27+
case KeyActions keys:
2628
writer.WriteString("type", "key");
2729
writer.WritePropertyName("actions");
28-
JsonSerializer.Serialize(writer, keys.Actions, options);
30+
JsonSerializer.Serialize(writer, keys.Actions.Select(a => a as Key), options);
2931

3032
break;
31-
case SourceActions.Pointers pointers:
33+
case PointerActions pointers:
3234
writer.WriteString("type", "pointer");
3335
if (pointers.Options is not null)
3436
{
@@ -37,23 +39,24 @@ public override void Write(Utf8JsonWriter writer, SourceActions value, JsonSeria
3739
}
3840

3941
writer.WritePropertyName("actions");
40-
JsonSerializer.Serialize(writer, pointers.Actions, options);
42+
JsonSerializer.Serialize(writer, pointers.Actions.Select(a => a as Pointer), options);
4143

4244
break;
43-
case SourceActions.Wheels wheels:
45+
case WheelActions wheels:
4446
writer.WriteString("type", "wheel");
4547
writer.WritePropertyName("actions");
46-
JsonSerializer.Serialize(writer, wheels.Actions, options);
48+
JsonSerializer.Serialize(writer, wheels.Actions.Select(a => a as Wheel), options);
4749

4850
break;
49-
case SourceActions.None none:
51+
case NoneActions none:
5052
writer.WriteString("type", "none");
5153
writer.WritePropertyName("actions");
52-
JsonSerializer.Serialize(writer, none.Actions, options);
54+
JsonSerializer.Serialize(writer, none.Actions.Select(a => a as None), options);
5355

5456
break;
5557
}
5658

5759
writer.WriteEndObject();
5860
}
5961
}
62+

dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextInputModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace OpenQA.Selenium.BiDi.Modules.BrowsingContext;
88

99
public class BrowsingContextInputModule(BrowsingContext context, InputModule inputModule)
1010
{
11-
public Task PerformActionsAsync(IEnumerable<SourceActions> actions, PerformActionsOptions? options = null)
11+
public Task PerformActionsAsync(IEnumerable<ISourceActions> actions, PerformActionsOptions? options = null)
1212
{
1313
return inputModule.PerformActionsAsync(context, actions, options);
1414
}

dotnet/src/webdriver/BiDi/Modules/Input/InputModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace OpenQA.Selenium.BiDi.Modules.Input;
88

99
public sealed class InputModule(Broker broker) : Module(broker)
1010
{
11-
public async Task PerformActionsAsync(BrowsingContext.BrowsingContext context, IEnumerable<SourceActions> actions, PerformActionsOptions? options = null)
11+
public async Task PerformActionsAsync(BrowsingContext.BrowsingContext context, IEnumerable<ISourceActions> actions, PerformActionsOptions? options = null)
1212
{
1313
var @params = new PerformActionsCommandParameters(context, actions);
1414

dotnet/src/webdriver/BiDi/Modules/Input/PerformActionsCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ namespace OpenQA.Selenium.BiDi.Modules.Input;
77

88
internal class PerformActionsCommand(PerformActionsCommandParameters @params) : Command<PerformActionsCommandParameters>(@params);
99

10-
internal record PerformActionsCommandParameters(BrowsingContext.BrowsingContext Context, IEnumerable<SourceActions> Actions) : CommandParameters;
10+
internal record PerformActionsCommandParameters(BrowsingContext.BrowsingContext Context, IEnumerable<ISourceActions> Actions) : CommandParameters;
1111

1212
public record PerformActionsOptions : CommandOptions;

dotnet/src/webdriver/BiDi/Modules/Input/SourceActions.cs

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,45 @@
77

88
namespace OpenQA.Selenium.BiDi.Modules.Input;
99

10-
public abstract record SourceActions
10+
public interface ISourceActions
1111
{
12-
public string Id { get; } = Guid.NewGuid().ToString();
13-
14-
public record Keys : SourceActions, IEnumerable<Key>
15-
{
16-
public IList<Key> Actions { get; set; } = [];
12+
string Id { get; }
1713

18-
public void Add(Key key) => Actions.Add(key);
19-
20-
public IEnumerator<Key> GetEnumerator() => Actions.GetEnumerator();
21-
22-
IEnumerator IEnumerable.GetEnumerator() => Actions.GetEnumerator();
23-
}
24-
25-
public record Pointers : SourceActions, IEnumerable<Pointer>
26-
{
27-
public PointerParameters? Options { get; set; }
28-
29-
public IList<Pointer> Actions { get; set; } = [];
30-
31-
public void Add(Pointer pointer) => Actions.Add(pointer);
14+
IList<ISourceAction> Actions { get; }
15+
}
3216

33-
public IEnumerator<Pointer> GetEnumerator() => Actions.GetEnumerator();
17+
public abstract record SourceActions<T> : ISourceActions, IEnumerable<ISourceAction>
18+
{
19+
public string Id { get; } = Guid.NewGuid().ToString();
3420

35-
IEnumerator IEnumerable.GetEnumerator() => Actions.GetEnumerator();
36-
}
21+
[JsonPropertyName("actions")]
22+
public IList<ISourceAction> Actions { get; } = [];
3723

38-
public record Wheels : SourceActions, IEnumerable<Wheel>
39-
{
40-
public IList<Wheel> Actions { get; set; } = [];
24+
public IEnumerator<ISourceAction> GetEnumerator() => Actions.GetEnumerator();
4125

42-
public void Add(Wheel wheel) => Actions.Add(wheel);
26+
IEnumerator IEnumerable.GetEnumerator() => Actions.GetEnumerator();
4327

44-
public IEnumerator<Wheel> GetEnumerator() => Actions.GetEnumerator();
28+
public void Add(ISourceAction action) => Actions.Add(action);
29+
}
4530

46-
IEnumerator IEnumerable.GetEnumerator() => Actions.GetEnumerator();
47-
}
31+
public record KeyActions : SourceActions<Key>, ISourceActions;
4832

49-
public record None : SourceActions, IEnumerable<Input.None>
50-
{
51-
public IList<Input.None> Actions { get; set; } = [];
33+
public record PointerActions : SourceActions<Pointer>, ISourceActions
34+
{
35+
public PointerParameters? Options { get; set; }
36+
}
5237

53-
public void Add(Input.None none) => Actions.Add(none);
38+
public record WheelActions : SourceActions<Wheel>;
5439

55-
public IEnumerator<Input.None> GetEnumerator() => Actions.GetEnumerator();
40+
public record NoneActions : SourceActions<None>;
5641

57-
IEnumerator IEnumerable.GetEnumerator() => Actions.GetEnumerator();
58-
}
59-
}
42+
public interface ISourceAction;
6043

6144
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
6245
[JsonDerivedType(typeof(Pause), "pause")]
6346
[JsonDerivedType(typeof(Down), "keyDown")]
6447
[JsonDerivedType(typeof(Up), "keyUp")]
65-
public abstract record Key
48+
public abstract record Key : ISourceAction
6649
{
6750
public record Pause : Key
6851
{
@@ -79,7 +62,7 @@ public record Up(string Value) : Key;
7962
[JsonDerivedType(typeof(Down), "pointerDown")]
8063
[JsonDerivedType(typeof(Up), "pointerUp")]
8164
[JsonDerivedType(typeof(Move), "pointerMove")]
82-
public abstract record Pointer
65+
public abstract record Pointer : ISourceAction
8366
{
8467
public record Pause : Pointer
8568
{
@@ -118,7 +101,7 @@ public record Move(int X, int Y) : Pointer, IPointerCommonProperties
118101
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
119102
[JsonDerivedType(typeof(Pause), "pause")]
120103
[JsonDerivedType(typeof(Scroll), "scroll")]
121-
public abstract record Wheel
104+
public abstract record Wheel : ISourceAction
122105
{
123106
public record Pause : Wheel
124107
{
@@ -135,7 +118,7 @@ public record Scroll(int X, int Y, int DeltaX, int DeltaY) : Wheel
135118

136119
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
137120
[JsonDerivedType(typeof(Pause), "pause")]
138-
public abstract record None
121+
public abstract record None : ISourceAction
139122
{
140123
public record Pause : None
141124
{

dotnet/test/common/BiDi/Input/CombinedInputActionsTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ public async Task Paint()
1414

1515
await Task.Delay(3000);
1616

17-
await context.Input.PerformActionsAsync([new SourceActions.Pointers {
17+
await context.Input.PerformActionsAsync([new PointerActions {
1818
new Pointer.Move(300, 300),
1919
new Pointer.Down(0),
2020
new Pointer.Move(400, 400) { Duration = 2000, Width = 1, Twist = 1 },
2121
new Pointer.Up(0),
2222
}]);
2323

24-
await context.Input.PerformActionsAsync([new SourceActions.Keys {
24+
await context.Input.PerformActionsAsync([new KeyActions {
2525
new Key.Down("U"),
2626
new Key.Up("U")
2727
}]);
2828

29-
await context.Input.PerformActionsAsync([new SourceActions.Pointers {
29+
await context.Input.PerformActionsAsync([new PointerActions {
3030
new Pointer.Move(300, 300),
3131
new Pointer.Down(0),
3232
new Pointer.Move(400, 400) { Duration = 2000 },
@@ -44,7 +44,7 @@ public async Task TestShiftClickingOnMultiSelectionList()
4444
var options = await context.LocateNodesAsync(new Locator.Css("option"));
4545

4646
await context.Input.PerformActionsAsync([
47-
new SourceActions.Pointers
47+
new PointerActions
4848
{
4949
new Pointer.Down(1),
5050
new Pointer.Up(1),

dotnet/test/common/BiDi/Input/DefaultMouseTest.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@ public async Task PerformDragAndDropWithMouse()
1212
driver.Url = UrlBuilder.WhereIs("draggableLists.html");
1313

1414
await context.Input.PerformActionsAsync([
15-
new SourceActions.Keys
15+
new KeyActions
1616
{
1717
Actions =
1818
{
19-
new Key.Down("A")
19+
new Key.Down("A"),
20+
new Key.Up("B")
2021
}
2122
}
2223
]);
2324

24-
await context.Input.PerformActionsAsync([new SourceActions.Keys
25+
await context.Input.PerformActionsAsync([new KeyActions
2526
{
2627
new Key.Down("A"),
2728
new Key.Down("B"),
2829
new Key.Pause()
2930
}]);
3031

31-
await context.Input.PerformActionsAsync([new SourceActions.Pointers
32+
await context.Input.PerformActionsAsync([new PointerActions
3233
{
3334
new Pointer.Down(0),
3435
new Pointer.Up(0),

0 commit comments

Comments
 (0)