Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
coenm committed Oct 26, 2024
1 parent d77a43e commit 0bf9031
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 35 deletions.
1 change: 1 addition & 0 deletions tests/UiTests/Class1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace UiTests;
using UiTests.Framework;
using UiTests.RepoM;
using UiTests.Utils;
using UiTests.VisualStudioCode;
using Xunit;
using Xunit.Abstractions;

Expand Down
3 changes: 3 additions & 0 deletions tests/UiTests/VisualStudioCode/Position.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace UiTests.VisualStudioCode;

public record Position(int Line, int Column);
49 changes: 49 additions & 0 deletions tests/UiTests/VisualStudioCode/VisualStudioPositionElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace UiTests.VisualStudioCode;

using System;
using System.Text.RegularExpressions;
using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using Xunit.Abstractions;

public partial class VisualStudioPositionElement : AutomationElement
{
private readonly ITestOutputHelper? _outputHelper;

public VisualStudioPositionElement(FrameworkAutomationElementBase frameworkAutomationElement) : base(frameworkAutomationElement)
{
}

public VisualStudioPositionElement(AutomationElement automationElement, ITestOutputHelper outputHelper)
: base(automationElement)
{
_outputHelper = outputHelper;
}

public Position Position
{
get
{
var text = Name.Trim();

if (string.IsNullOrWhiteSpace(text))
{
throw new NotSupportedException("Could not find line and column from empty text");
}

Match result = RegexLineCol().Match(text);
if (result.Success)
{
return new Position(
int.Parse(result.Groups[1].Value),
int.Parse(result.Groups[2].Value));
}

throw new NotSupportedException($"Could not find line and column from text '{text}'");
}
}

// TODO: also other type Ln 3, Col 4 (3 selected) stuf.
[GeneratedRegex("Ln\\s([0-9]+),\\sCol\\s([0-9]+)", RegexOptions.Compiled | RegexOptions.Singleline)]
private static partial Regex RegexLineCol();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace UiTests.RepoM;
namespace UiTests.VisualStudioCode;

using System;
using System.Text.RegularExpressions;
Expand All @@ -14,8 +14,6 @@ namespace UiTests.RepoM;
using AutomationElement = FlaUI.Core.AutomationElements.AutomationElement;
using NotSupportedException = FlaUI.Core.Exceptions.NotSupportedException;

public record Position(int Line, int Column);

public class VsCodeWindow : Window
{
private readonly ITestOutputHelper _outputHelper;
Expand All @@ -26,7 +24,7 @@ public VsCodeWindow(FrameworkAutomationElementBase frameworkAutomationElement, I
_outputHelper = outputHelper ?? throw new ArgumentNullException(nameof(outputHelper));
}

public Position CurrentCursorPosition
public VisualStudioPositionElement PositionElement
{
get
{
Expand All @@ -35,28 +33,12 @@ public Position CurrentCursorPosition

var children = selection.FindAllChildren();
children.Should().HaveCount(1);

var text = children[0].Name.Trim();

if (string.IsNullOrWhiteSpace(text))
{
throw new NotSupportedException("Could not find line and column from empty text");
}

// todo, also other type Ln 3, Col 4 (3, 4) stuf.
var regex = new Regex("Ln\\s([0-9]+),\\sCol\\s([0-9]+)", RegexOptions.Compiled | RegexOptions.Singleline);
Match result = regex.Match(text);
if (result.Success)
{
return new Position(
int.Parse(result.Groups[1].Value),
int.Parse(result.Groups[2].Value));
}

throw new NotSupportedException("Could not find line and column from " + text);
return new VisualStudioPositionElement(children[0], _outputHelper);
}
}


public Button NotificationButton
{
get
Expand Down Expand Up @@ -89,16 +71,17 @@ public int Indentation
// only one child should be present
item.FindAllChildren().Should().HaveCount(1);
var text = item.FindAllChildren()[0].Name.Trim();

_outputHelper.WriteLine("Indentation: " + text);

return text.Length; //todo
}
}

public async Task<Position> GetCurrentCursorPositionAsync(Predicate<Position>? untilCheck = null)
{
Position result = CurrentCursorPosition;
VisualStudioPositionElement positionElement = PositionElement;
Position result = positionElement.Position;

if (untilCheck == null)
{
Expand All @@ -113,7 +96,7 @@ public async Task<Position> GetCurrentCursorPositionAsync(Predicate<Position>? u
counter++;
try
{
result = CurrentCursorPosition;
result = positionElement.Position;
}
catch (Exception e)
{
Expand All @@ -123,7 +106,7 @@ public async Task<Position> GetCurrentCursorPositionAsync(Predicate<Position>? u

return result;
}

public async Task FocusActiveEditorGroupAsync()
{
await VsCodeOpenCommandPalletAsync();
Expand All @@ -134,7 +117,7 @@ public async Task FocusActiveEditorGroupAsync()
await Task.Delay(100);
}

private Task VsCodeOpenCommandPalletAsync()
private Task VsCodeOpenCommandPalletAsync()
{
Keyboard.TypeSimultaneously(
VirtualKeyShort.CONTROL,
Expand All @@ -147,7 +130,7 @@ public async Task FocusUsingMouseAsync()
{
this.WaitUntilClickable(TimeSpan.FromSeconds(5));

if (!this.TryGetClickablePoint(out var p))
if (!TryGetClickablePoint(out var p))
{
throw new InvalidOperationException("Could not get clickable point");
}
Expand All @@ -161,7 +144,7 @@ public async Task FocusUsingMouseAsync()

public Task GoToLineAsync(int lineNumber)
{
var pos = CurrentCursorPosition;
Position pos = PositionElement.Position;
if (pos.Line == lineNumber)
{
return Task.CompletedTask;
Expand All @@ -173,7 +156,7 @@ public Task GoToLineAsync(int lineNumber)
}

return GoToLineUsingCtrlGAsync(lineNumber);

}

private async Task GoToLineUsingCtrlGAsync(int lineNumber)
Expand All @@ -198,21 +181,21 @@ private async Task GoToLineUsingArrow(int lineNumber)

if (currentPos.Line < lineNumber)
{
for (int i = 0; i < delta; i++)
for (var i = 0; i < delta; i++)
{
Keyboard.Type(VirtualKeyShort.DOWN);
await Task.Delay(20);
}
}
else
{
for (int i = 0; i < delta; i++)
for (var i = 0; i < delta; i++)
{
Keyboard.Type(VirtualKeyShort.UP);
await Task.Delay(20);
}
}

currentPos = await GetCurrentCursorPositionAsync(c => c.Line == lineNumber);
}
}
Expand Down

0 comments on commit 0bf9031

Please sign in to comment.