Skip to content

Commit

Permalink
[dotnet] Annotate nullable reference types on ActionBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
RenderMichael committed Dec 2, 2024
1 parent 4f07e4a commit d17a3b9
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions dotnet/src/webdriver/Interactions/ActionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using System.Collections.Generic;
using System.Text;

#nullable enable

namespace OpenQA.Selenium.Interactions
{
/// <summary>
Expand All @@ -29,7 +31,7 @@ namespace OpenQA.Selenium.Interactions
/// </summary>
public class ActionBuilder
{
private Dictionary<InputDevice, ActionSequence> sequences = new Dictionary<InputDevice, ActionSequence>();
private readonly Dictionary<InputDevice, ActionSequence> sequences = new Dictionary<InputDevice, ActionSequence>();

/// <summary>
/// Adds an action to the built set of actions. Adding an action will
Expand All @@ -39,7 +41,7 @@ public class ActionBuilder
/// <returns>A self reference.</returns>
public ActionBuilder AddAction(Interaction actionToAdd)
{
this.AddActions(actionToAdd);
this.ProcessTick(actionToAdd);
return this;
}

Expand Down Expand Up @@ -71,7 +73,7 @@ public IList<ActionSequence> ToActionSequenceList()
/// </summary>
public void ClearSequences()
{
this.sequences = new Dictionary<InputDevice, ActionSequence>();
this.sequences.Clear();
}

/// <summary>
Expand All @@ -89,13 +91,26 @@ public override string ToString()
return builder.ToString();
}

private void ProcessTick(Interaction interaction)
{
ActionSequence sequence = this.GetOrAddSequence(interaction.SourceDevice);
sequence.AddAction(interaction);

foreach (KeyValuePair<InputDevice, ActionSequence> pair in this.sequences)
{
if (pair.Key != interaction.SourceDevice)
{
pair.Value.AddAction(new PauseInteraction(pair.Key, TimeSpan.Zero));
}
}
}

private void ProcessTick(params Interaction[] interactionsToAdd)
{
List<InputDevice> usedDevices = new List<InputDevice>();
foreach (Interaction interaction in interactionsToAdd)
{
InputDevice actionDevice = interaction.SourceDevice;
if (usedDevices.Contains(actionDevice))
if (usedDevices.Contains(interaction.SourceDevice))
{
throw new ArgumentException("You can only add one action per device for a single tick.");
}
Expand All @@ -104,8 +119,9 @@ private void ProcessTick(params Interaction[] interactionsToAdd)
List<InputDevice> unusedDevices = new List<InputDevice>(this.sequences.Keys);
foreach (Interaction interaction in interactionsToAdd)
{
ActionSequence sequence = this.FindSequence(interaction.SourceDevice);
ActionSequence sequence = this.GetOrAddSequence(interaction.SourceDevice);
sequence.AddAction(interaction);

unusedDevices.Remove(interaction.SourceDevice);
}

Expand All @@ -116,11 +132,11 @@ private void ProcessTick(params Interaction[] interactionsToAdd)
}
}

private ActionSequence FindSequence(InputDevice device)
private ActionSequence GetOrAddSequence(InputDevice device)
{
if (this.sequences.ContainsKey(device))
if (this.sequences.TryGetValue(device, out ActionSequence? existingSequence))
{
return this.sequences[device];
return existingSequence;
}

int longestSequenceLength = 0;
Expand Down

0 comments on commit d17a3b9

Please sign in to comment.