Fix smoke 1차 follow-up gaps: player resolver, type target, filter, utf8 (#12)
This commit is contained in:
135
tests/Recordingtest.Player.Tests/UiaPathResolverTests.cs
Normal file
135
tests/Recordingtest.Player.Tests/UiaPathResolverTests.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace Recordingtest.Player.Tests;
|
||||
|
||||
public class UiaPathResolverTests
|
||||
{
|
||||
private sealed class FakeNode : IUiaTreeNode
|
||||
{
|
||||
public string ClassName { get; set; } = "";
|
||||
public string? AutomationId { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public ElementBounds Bounds { get; set; }
|
||||
public List<FakeNode> ChildList { get; } = new();
|
||||
public IEnumerable<IUiaTreeNode> Children => ChildList;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UiaPathParser_ParsesMultiSegment_WithClassAndId()
|
||||
{
|
||||
var segs = UiaPathParser.Parse(
|
||||
"MetroWindow[@AutomationId='root']/CommandPanel/TextBox[@AutomationId='CommandBox']/TextBox[@AutomationId='CB']");
|
||||
Assert.Equal(4, segs.Count);
|
||||
Assert.Equal("MetroWindow", segs[0].ClassName);
|
||||
Assert.Equal("root", segs[0].AutomationId);
|
||||
Assert.Equal("CommandPanel", segs[1].ClassName);
|
||||
Assert.Null(segs[1].AutomationId);
|
||||
Assert.Equal("TextBox", segs[2].ClassName);
|
||||
Assert.Equal("CommandBox", segs[2].AutomationId);
|
||||
Assert.Equal("CB", segs[3].AutomationId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UiaPathParser_ParsesNameAttribute()
|
||||
{
|
||||
var seg = UiaPathParser.ParseSegment("Button[@Name='새 파일']");
|
||||
Assert.Equal("Button", seg.ClassName);
|
||||
Assert.Equal("새 파일", seg.Name);
|
||||
Assert.Null(seg.AutomationId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UiaPathResolver_Descend_FindsNestedElement()
|
||||
{
|
||||
var leaf = new FakeNode
|
||||
{
|
||||
ClassName = "TextBox",
|
||||
AutomationId = "CB",
|
||||
Bounds = new ElementBounds(10, 20, 30, 40),
|
||||
};
|
||||
var commandBox = new FakeNode
|
||||
{
|
||||
ClassName = "TextBox",
|
||||
AutomationId = "CommandBox",
|
||||
};
|
||||
commandBox.ChildList.Add(leaf);
|
||||
var panel = new FakeNode { ClassName = "CommandPanel" };
|
||||
panel.ChildList.Add(commandBox);
|
||||
var root = new FakeNode { ClassName = "MetroWindow", AutomationId = "root" };
|
||||
root.ChildList.Add(panel);
|
||||
|
||||
var found = UiaPathResolver.Resolve(
|
||||
root,
|
||||
"MetroWindow[@AutomationId='root']/CommandPanel/TextBox[@AutomationId='CommandBox']/TextBox[@AutomationId='CB']");
|
||||
|
||||
Assert.NotNull(found);
|
||||
Assert.Equal("CB", found!.AutomationId);
|
||||
Assert.Equal(10, found.Bounds.X);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UiaPathResolver_LastSegmentWithoutId_UsesClassName()
|
||||
{
|
||||
var items = new FakeNode
|
||||
{
|
||||
ClassName = "ItemsControl",
|
||||
Bounds = new ElementBounds(0, 0, 1920, 1040),
|
||||
};
|
||||
var root = new FakeNode { ClassName = "MetroWindow", AutomationId = "root" };
|
||||
root.ChildList.Add(items);
|
||||
|
||||
var found = UiaPathResolver.Resolve(
|
||||
root,
|
||||
"MetroWindow[@AutomationId='root']/ItemsControl");
|
||||
|
||||
Assert.NotNull(found);
|
||||
Assert.Equal("ItemsControl", found!.ClassName);
|
||||
Assert.Equal(1920, found.Bounds.Width);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UiaPathResolver_NotFound_ReturnsNull()
|
||||
{
|
||||
var root = new FakeNode { ClassName = "MetroWindow", AutomationId = "root" };
|
||||
var found = UiaPathResolver.Resolve(
|
||||
root,
|
||||
"MetroWindow[@AutomationId='root']/Missing");
|
||||
Assert.Null(found);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SmokeRegression_BoxV4CleanLike_ParsesAndResolves()
|
||||
{
|
||||
// Build a minimal fake tree resembling EG-BIM Modeler.
|
||||
var cb = new FakeNode { ClassName = "TextBox", AutomationId = "CB", Bounds = new ElementBounds(400, 1020, 200, 30) };
|
||||
var commandBox = new FakeNode { ClassName = "TextBox", AutomationId = "CommandBox" };
|
||||
commandBox.ChildList.Add(cb);
|
||||
var cmdPanel = new FakeNode { ClassName = "CommandPanel" };
|
||||
cmdPanel.ChildList.Add(commandBox);
|
||||
var items = new FakeNode { ClassName = "ItemsControl", Bounds = new ElementBounds(0, 0, 1920, 1040) };
|
||||
var root = new FakeNode { ClassName = "MetroWindow", AutomationId = "root" };
|
||||
root.ChildList.Add(cmdPanel);
|
||||
root.ChildList.Add(items);
|
||||
|
||||
var paths = new[]
|
||||
{
|
||||
"MetroWindow[@AutomationId='root']/ItemsControl",
|
||||
"MetroWindow[@AutomationId='root']/CommandPanel/TextBox[@AutomationId='CommandBox']/TextBox[@AutomationId='CB']",
|
||||
};
|
||||
|
||||
foreach (var p in paths)
|
||||
{
|
||||
var n = UiaPathResolver.Resolve(root, p);
|
||||
Assert.NotNull(n);
|
||||
}
|
||||
|
||||
// The two paths should resolve to *different* nodes — proving the
|
||||
// resolver no longer collapses to "first descendant".
|
||||
var n1 = UiaPathResolver.Resolve(root, paths[0])!;
|
||||
var n2 = UiaPathResolver.Resolve(root, paths[1])!;
|
||||
Assert.NotSame(n1, n2);
|
||||
Assert.Equal("ItemsControl", n1.ClassName);
|
||||
Assert.Equal("CB", n2.AutomationId);
|
||||
}
|
||||
}
|
||||
@@ -299,6 +299,138 @@ public class RecorderTests
|
||||
Assert.Equal(0x43u, steps[0].RawVk);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DragCollapser_TypeAfterFocusChange_InheritsTarget()
|
||||
{
|
||||
// focus_change → key_down B,O,X (no resolver result for keys).
|
||||
UiaResolution? Resolver(RawEvent ev) => null;
|
||||
const string focusPath = "MetroWindow[@AutomationId='root']/CommandPanel/TextBox[@AutomationId='CommandBox']/TextBox[@AutomationId='CB']";
|
||||
var events = new[]
|
||||
{
|
||||
new RawEvent(100, "focus_change", 0, 0, 0, 0, focusPath),
|
||||
new RawEvent(110, "key_down", 0, 0, 0x42, 0), // B
|
||||
new RawEvent(120, "key_down", 0, 0, 0x4F, 0), // O
|
||||
new RawEvent(130, "key_down", 0, 0, 0x58, 0), // X
|
||||
};
|
||||
|
||||
var steps = new DragCollapser().Collapse(events, Resolver);
|
||||
|
||||
// focus + type
|
||||
Assert.Equal(2, steps.Count);
|
||||
Assert.Equal("type", steps[1].Kind);
|
||||
Assert.Equal("BOX", steps[1].Value);
|
||||
Assert.NotNull(steps[1].Target);
|
||||
Assert.Equal(focusPath, steps[1].Target!.UiaPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DragCollapser_TypeAfterMouseDown_FallbackToMouseTarget()
|
||||
{
|
||||
var el = MakeRectElement("Canvas", 0, 0, 800, 600);
|
||||
const string mousePath = "Window[@Name='Canvas']";
|
||||
UiaResolution? Resolver(RawEvent ev) =>
|
||||
ev.Kind == "key_down" || ev.Kind == "key_up"
|
||||
? null
|
||||
: new UiaResolution(el, mousePath);
|
||||
|
||||
var events = new[]
|
||||
{
|
||||
new RawEvent(100, "mouse_down_l", 100, 100, 0, 0),
|
||||
new RawEvent(105, "mouse_up_l", 101, 101, 0, 0),
|
||||
new RawEvent(110, "key_down", 0, 0, 0x31, 0), // 1
|
||||
new RawEvent(120, "key_down", 0, 0, 0x30, 0), // 0
|
||||
};
|
||||
|
||||
var steps = new DragCollapser().Collapse(events, Resolver);
|
||||
|
||||
Assert.Equal(2, steps.Count);
|
||||
Assert.Equal("click", steps[0].Kind);
|
||||
Assert.Equal("type", steps[1].Kind);
|
||||
Assert.Equal("10", steps[1].Value);
|
||||
Assert.NotNull(steps[1].Target);
|
||||
Assert.Equal(mousePath, steps[1].Target!.UiaPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WindowFilter_ExternalCoord_DropsEvent()
|
||||
{
|
||||
// SUT pid is 42. Mouse event at (10,10) belongs to pid 99 → drop.
|
||||
var filter = new SutProcessWindowFilter(
|
||||
sutPid: 42,
|
||||
processFromPoint: (_, _) => 99,
|
||||
processFromForeground: () => 99);
|
||||
|
||||
var ev = new RawEvent(0, "mouse_down_l", 10, 10, 0, 0);
|
||||
Assert.False(filter.ShouldKeep(ev));
|
||||
|
||||
var key = new RawEvent(0, "key_down", 0, 0, 0x42, 0);
|
||||
Assert.False(filter.ShouldKeep(key));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WindowFilter_SutCoord_KeepsEvent()
|
||||
{
|
||||
var filter = new SutProcessWindowFilter(
|
||||
sutPid: 42,
|
||||
processFromPoint: (_, _) => 42,
|
||||
processFromForeground: () => 42);
|
||||
|
||||
Assert.True(filter.ShouldKeep(new RawEvent(0, "mouse_down_l", 10, 10, 0, 0)));
|
||||
Assert.True(filter.ShouldKeep(new RawEvent(0, "key_down", 0, 0, 0x42, 0)));
|
||||
// Unknown pid (0) is permissively kept to avoid false drops.
|
||||
var permissive = new SutProcessWindowFilter(
|
||||
sutPid: 42,
|
||||
processFromPoint: (_, _) => 0,
|
||||
processFromForeground: () => 0);
|
||||
Assert.True(permissive.ShouldKeep(new RawEvent(0, "mouse_down_l", 1, 1, 0, 0)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ScenarioWriter_RoundTrip_PreservesKorean()
|
||||
{
|
||||
var s = new Scenario
|
||||
{
|
||||
Name = "한글-시나리오",
|
||||
Description = "회귀 테스트 — 새 파일",
|
||||
Steps =
|
||||
{
|
||||
new ScenarioStep
|
||||
{
|
||||
Kind = "click",
|
||||
Target = new ScenarioTarget
|
||||
{
|
||||
UiaPath = "MetroWindow[@AutomationId='root']/Button[@Name='새 파일']",
|
||||
Offset = new[] { 0.5, 0.5 },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
var tmp = Path.Combine(Path.GetTempPath(), $"recordingtest-utf8-{System.Guid.NewGuid():N}.yaml");
|
||||
try
|
||||
{
|
||||
ScenarioWriter.WriteToFile(s, tmp);
|
||||
|
||||
// No BOM at byte 0.
|
||||
var bytes = File.ReadAllBytes(tmp);
|
||||
Assert.True(bytes.Length >= 3);
|
||||
Assert.False(bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF,
|
||||
"ScenarioWriter must emit UTF-8 without BOM");
|
||||
|
||||
var text = File.ReadAllText(tmp, System.Text.Encoding.UTF8);
|
||||
var parsed = ScenarioWriter.Deserialize(text);
|
||||
|
||||
Assert.Equal(s.Name, parsed.Name);
|
||||
Assert.Equal(s.Description, parsed.Description);
|
||||
Assert.Equal("MetroWindow[@AutomationId='root']/Button[@Name='새 파일']",
|
||||
parsed.Steps[0].Target!.UiaPath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tmp)) File.Delete(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Cli_MissingAttach_ExitTwo()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user