2121using System . Collections . Generic ;
2222using System . Text ;
2323
24+ #nullable enable
25+
2426namespace OpenQA . Selenium . Interactions
2527{
2628 /// <summary>
@@ -29,7 +31,7 @@ namespace OpenQA.Selenium.Interactions
2931 /// </summary>
3032 public class ActionBuilder
3133 {
32- private Dictionary < InputDevice , ActionSequence > sequences = new Dictionary < InputDevice , ActionSequence > ( ) ;
34+ private readonly Dictionary < InputDevice , ActionSequence > sequences = new Dictionary < InputDevice , ActionSequence > ( ) ;
3335
3436 /// <summary>
3537 /// Adds an action to the built set of actions. Adding an action will
@@ -39,7 +41,7 @@ public class ActionBuilder
3941 /// <returns>A self reference.</returns>
4042 public ActionBuilder AddAction ( Interaction actionToAdd )
4143 {
42- this . AddActions ( actionToAdd ) ;
44+ this . ProcessTick ( actionToAdd ) ;
4345 return this ;
4446 }
4547
@@ -71,7 +73,7 @@ public IList<ActionSequence> ToActionSequenceList()
7173 /// </summary>
7274 public void ClearSequences ( )
7375 {
74- this . sequences = new Dictionary < InputDevice , ActionSequence > ( ) ;
76+ this . sequences . Clear ( ) ;
7577 }
7678
7779 /// <summary>
@@ -89,13 +91,26 @@ public override string ToString()
8991 return builder . ToString ( ) ;
9092 }
9193
94+ private void ProcessTick ( Interaction interaction )
95+ {
96+ ActionSequence sequence = this . GetOrAddSequence ( interaction . SourceDevice ) ;
97+ sequence . AddAction ( interaction ) ;
98+
99+ foreach ( KeyValuePair < InputDevice , ActionSequence > pair in this . sequences )
100+ {
101+ if ( pair . Key != interaction . SourceDevice )
102+ {
103+ pair . Value . AddAction ( new PauseInteraction ( pair . Key , TimeSpan . Zero ) ) ;
104+ }
105+ }
106+ }
107+
92108 private void ProcessTick ( params Interaction [ ] interactionsToAdd )
93109 {
94110 List < InputDevice > usedDevices = new List < InputDevice > ( ) ;
95111 foreach ( Interaction interaction in interactionsToAdd )
96112 {
97- InputDevice actionDevice = interaction . SourceDevice ;
98- if ( usedDevices . Contains ( actionDevice ) )
113+ if ( usedDevices . Contains ( interaction . SourceDevice ) )
99114 {
100115 throw new ArgumentException ( "You can only add one action per device for a single tick." ) ;
101116 }
@@ -104,8 +119,9 @@ private void ProcessTick(params Interaction[] interactionsToAdd)
104119 List < InputDevice > unusedDevices = new List < InputDevice > ( this . sequences . Keys ) ;
105120 foreach ( Interaction interaction in interactionsToAdd )
106121 {
107- ActionSequence sequence = this . FindSequence ( interaction . SourceDevice ) ;
122+ ActionSequence sequence = this . GetOrAddSequence ( interaction . SourceDevice ) ;
108123 sequence . AddAction ( interaction ) ;
124+
109125 unusedDevices . Remove ( interaction . SourceDevice ) ;
110126 }
111127
@@ -116,11 +132,11 @@ private void ProcessTick(params Interaction[] interactionsToAdd)
116132 }
117133 }
118134
119- private ActionSequence FindSequence ( InputDevice device )
135+ private ActionSequence GetOrAddSequence ( InputDevice device )
120136 {
121- if ( this . sequences . ContainsKey ( device ) )
137+ if ( this . sequences . TryGetValue ( device , out ActionSequence ? existingSequence ) )
122138 {
123- return this . sequences [ device ] ;
139+ return existingSequence ;
124140 }
125141
126142 int longestSequenceLength = 0 ;
0 commit comments