Skip to content

Commit e3e1306

Browse files
committed
Predicate scripts should be invovked asynchronously
1 parent 86416c3 commit e3e1306

File tree

7 files changed

+20
-23
lines changed

7 files changed

+20
-23
lines changed

src/Pixel.Automation.Core.Components/Entities/ControlEntity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ protected T GetElementMatchingCriteria<T>(IEnumerable<T> foundControls)
203203
protected async Task<bool> ApplyPredicate<T>(string predicateScriptFile, T targetElement)
204204
{
205205
IScriptEngine scriptEngine = this.EntityManager.GetScriptEngine();
206-
var fn = await scriptEngine.CreateDelegateAsync<Func<IComponent, T, bool>>(predicateScriptFile);
207-
bool isMatch = fn(this, targetElement);
206+
var fn = await scriptEngine.CreateDelegateAsync<Func<IComponent, T, Task<bool>>>(predicateScriptFile);
207+
bool isMatch = await fn(this, targetElement);
208208
return isMatch;
209209
}
210210
}

src/Pixel.Automation.Core/Arguments/ArgumentExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public static class ArgumentExtensions
1515
private static readonly string setValueAction = "return ((Action<{0}>)SetValue);";
1616
private static readonly string getValueTemplate = "{0} GetValue(){1}{{{1} return default;{1}}}";
1717
private static readonly string getValueDelegate = "return ((Func<{0}>)GetValue);";
18-
private static readonly string predicateScriptTemplate = "bool IsMatch(IComponent current, {0} argument){1}{{{1} return false;{1}}}";
19-
private static readonly string predicateDelegate = "return ((Func<IComponent, {0}, bool>)IsMatch);";
18+
private static readonly string predicateScriptTemplate = "Task<bool> IsMatch(IComponent current, {0} argument){1}{{{1} return Task.FromResult(false);{1}}}";
19+
private static readonly string predicateDelegate = "return ((Func<IComponent, {0}, Task<bool>>)IsMatch);";
2020

2121
/// <summary>
2222
/// Get the value of the argument

src/Pixel.Automation.Window.Management.Components/FindChildWindowActorComponent.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,8 @@ public override async Task ActAsync()
162162
protected async Task<bool> ApplyPredicate(string predicateScriptFile, ApplicationWindow applicationWindow)
163163
{
164164
IScriptEngine scriptEngine = this.EntityManager.GetScriptEngine();
165-
166-
var fn = await scriptEngine.CreateDelegateAsync<Func<Core.Interfaces.IComponent, ApplicationWindow, bool>>(predicateScriptFile);
167-
168-
bool isMatch = fn(this, applicationWindow);
165+
var fn = await scriptEngine.CreateDelegateAsync<Func<Core.Interfaces.IComponent, ApplicationWindow, Task<bool>>>(predicateScriptFile);
166+
bool isMatch = await fn(this, applicationWindow);
169167
return isMatch;
170168

171169
}

src/Pixel.Automation.Window.Management.Components/FindDesktopWindowActorComponent.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public FilterMode FilterMode
7373
this.SetDispalyAttribute(nameof(Index), true);
7474
break;
7575
case FilterMode.Custom:
76-
this.Filter = new PredicateArgument<ApplicationWindow>();
76+
this.Filter = new PredicateArgument<ApplicationWindow>() { CanChangeType = false };
7777
this.SetDispalyAttribute(nameof(Filter), true);
7878
this.SetDispalyAttribute(nameof(Index), false);
7979
break;
@@ -154,9 +154,8 @@ public override async Task ActAsync()
154154
protected async Task<bool> ApplyPredicate(string predicateScriptFile, ApplicationWindow applicationWindow)
155155
{
156156
IScriptEngine scriptEngine = this.EntityManager.GetScriptEngine();
157-
var fn = await scriptEngine.CreateDelegateAsync<Func<Core.Interfaces.IComponent, ApplicationWindow, bool>>(predicateScriptFile);
158-
159-
bool isMatch = fn(this, applicationWindow);
157+
var fn = await scriptEngine.CreateDelegateAsync<Func<Core.Interfaces.IComponent, ApplicationWindow, Task<bool>>>(predicateScriptFile);
158+
bool isMatch = await fn(this, applicationWindow);
160159
return isMatch;
161160
}
162161
}

src/Unit.Tests/Pixel.Automation.Core.Tests/Arguments/ArgumentExtensionsFixture.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public async Task ValidateThatValueCanBeRetrievedFromAnArgumentUsingGetValueExte
2222
}
2323

2424
[Test]
25-
public void ValidatethatArgumentValueCanBeSetUsingSetValueExtensionMethod()
25+
public async Task ValidatethatArgumentValueCanBeSetUsingSetValueExtensionMethod()
2626
{
2727
var argument = new InArgument<int>();
2828
var argumentProcessor = Substitute.For<IArgumentProcessor>();
2929
argumentProcessor.When(x => x.SetValueAsync<int>(Arg.Any<Argument>(), Arg.Any<int>())).Do(x => argument.DefaultValue = x.ArgAt<int>(1));
3030

31-
argument.SetValue(argumentProcessor, 10);
31+
await argument.SetValue(argumentProcessor, 10);
3232

3333
Assert.AreEqual(10, argument.DefaultValue);
3434

@@ -58,7 +58,7 @@ public void ValidateThatInitialScriptCanBeGeneratedForOutArgument()
5858
public void ValidateThatInitialScriptCanBeGeneratedForPredicateArgument()
5959
{
6060
var argument = new PredicateArgument<int>();
61-
var expected = "using System;\r\nusing Pixel.Automation.Core.Interfaces;\r\nbool IsMatch(IComponent current, Int32 argument)\r\n{\r\n return false;\r\n}\r\nreturn ((Func<IComponent, Int32, bool>)IsMatch);";
61+
var expected = "using System;\r\nusing Pixel.Automation.Core.Interfaces;\r\nTask<bool> IsMatch(IComponent current, Int32 argument)\r\n{\r\n return Task.FromResult(false);\r\n}\r\nreturn ((Func<IComponent, Int32, Task<bool>>)IsMatch);";
6262
var result = argument.GenerateInitialScript();
6363

6464
Assert.AreEqual(expected, result);

src/Unit.Tests/Pixel.Automation.Window.Management.Components.Tests/FindChildWindowActorComponentFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ public async Task ValidateThatFindChildWindowActorCanLocateChildWindowByCustomFi
122122
argumentProcessor.GetValueAsync<ApplicationWindow>(Arg.Any<InArgument<ApplicationWindow>>()).Returns(parentWindow);
123123

124124
var scriptEngine = Substitute.For<IScriptEngine>();
125-
scriptEngine.CreateDelegateAsync<Func<IComponent, ApplicationWindow, bool>>(Arg.Any<string>())
125+
scriptEngine.CreateDelegateAsync<Func<IComponent, ApplicationWindow, Task<bool>>>(Arg.Any<string>())
126126
.Returns((c, a) =>
127127
{
128128
if(a.Equals(childWindowTwo))
129129
{
130-
return true;
130+
return Task.FromResult(true);
131131
}
132-
return false;
132+
return Task.FromResult(false);
133133
});
134134

135135
entityManager.GetArgumentProcessor().Returns(argumentProcessor);
@@ -156,7 +156,7 @@ public async Task ValidateThatFindChildWindowActorCanLocateChildWindowByCustomFi
156156
await argumentProcessor.Received(1).GetValueAsync<string>(Arg.Any<InArgument<string>>());
157157
await argumentProcessor.Received(1).GetValueAsync<ApplicationWindow>(Arg.Any<InArgument<ApplicationWindow>>());
158158
windowManager.Received(1).FindAllChildWindows(parentWindow, childWindowTitle, MatchType.Equals, true);
159-
await scriptEngine.Received(2).CreateDelegateAsync<Func<IComponent, ApplicationWindow, bool>>("FindWindow.csx"); // 1 for each window in collecton until match found
159+
await scriptEngine.Received(2).CreateDelegateAsync<Func<IComponent, ApplicationWindow, Task<bool>>>("FindWindow.csx"); // 1 for each window in collecton until match found
160160
await argumentProcessor.Received(1).SetValueAsync<ApplicationWindow>(Arg.Any<OutArgument<ApplicationWindow>>(), childWindowTwo);
161161
}
162162
}

src/Unit.Tests/Pixel.Automation.Window.Management.Components.Tests/FindDesktopWindowActorComponentFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ public async Task ValidateThatFindChildWindowActorCanLocateChildWindowByCustomFi
118118
argumentProcessor.GetValueAsync<string>(Arg.Any<InArgument<string>>()).Returns(windowTitle);
119119

120120
var scriptEngine = Substitute.For<IScriptEngine>();
121-
scriptEngine.CreateDelegateAsync<Func<IComponent, ApplicationWindow, bool>>(Arg.Any<string>())
121+
scriptEngine.CreateDelegateAsync<Func<IComponent, ApplicationWindow, Task<bool>>>(Arg.Any<string>())
122122
.Returns((c, a) =>
123123
{
124124
if (a.Equals(windowTwo))
125125
{
126-
return true;
126+
return Task.FromResult(true);
127127
}
128-
return false;
128+
return Task.FromResult(false);
129129
});
130130

131131
entityManager.GetArgumentProcessor().Returns(argumentProcessor);
@@ -150,7 +150,7 @@ public async Task ValidateThatFindChildWindowActorCanLocateChildWindowByCustomFi
150150

151151
await argumentProcessor.Received(1).GetValueAsync<string>(Arg.Any<InArgument<string>>());
152152
windowManager.Received(1).FindAllDesktopWindows(windowTitle, MatchType.Equals, true);
153-
await scriptEngine.Received(2).CreateDelegateAsync<Func<IComponent, ApplicationWindow, bool>>("FindWindow.csx"); // 1 for each window in collecton until match found
153+
await scriptEngine.Received(2).CreateDelegateAsync<Func<IComponent, ApplicationWindow, Task<bool>>>("FindWindow.csx"); // 1 for each window in collecton until match found
154154
await argumentProcessor.Received(1).SetValueAsync<ApplicationWindow>(Arg.Any<OutArgument<ApplicationWindow>>(), windowTwo);
155155

156156

0 commit comments

Comments
 (0)