完善 XPath 定位与控件监控体验

- 增加简明到深入的 XPath 定位教程
- 保留增量控件树刷新与可编辑 Path
- 完善悬浮条条件选择和诊断

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
zimoyin
2026-10-13 13:00:10 +08:00
parent ec785c4fb6
commit f31b50bb36
51 changed files with 8491 additions and 1931 deletions
+160
View File
@@ -0,0 +1,160 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using pacs_win_uia.Models;
using pacs_win_uia.Services;
namespace PacsWinUia.RegressionTests
{
internal static class Program
{
[STAThread]
private static int Main()
{
Console.OutputEncoding = new UTF8Encoding(false);
var tests = new List<KeyValuePair<string, Action>>();
PathTests.Register(tests);
PersistenceTests.Register(tests);
TreeRefreshTests.Register(tests);
FloatingConditionsTests.Register(tests);
int passed = 0;
foreach (var test in tests)
{
try
{
test.Value();
passed++;
Console.WriteLine("PASS " + test.Key);
}
catch (Exception exception)
{
Console.WriteLine("FAIL " + test.Key);
Console.WriteLine(exception);
}
}
Console.WriteLine("RESULT total={0} passed={1} failed={2}", tests.Count, passed, tests.Count - passed);
return passed == tests.Count ? 0 : 1;
}
}
internal static class Check
{
public static void True(bool condition, string message)
{
if (!condition) throw new InvalidOperationException(message);
}
public static void Equal<T>(T expected, T actual, string message)
{
True(EqualityComparer<T>.Default.Equals(expected, actual),
message + "; expected=" + expected + "; actual=" + actual);
}
public static void Same(object expected, object actual, string message)
{
True(ReferenceEquals(expected, actual), message + "; node reference mismatch");
}
public static void Matches(ControlTreeNode root, string path, params ControlTreeNode[] expected)
{
string error;
IList<ControlTreeNode> actual = ControlPath.Select(root, path, out error);
Equal<string>(null, error, "Select error: " + path);
Equal(expected.Length, actual.Count, "Select count: " + path);
for (int i = 0; i < expected.Length; i++) Same(expected[i], actual[i], "Select order: " + path);
if (expected.Length == 1)
{
Same(expected[0], ControlPath.SelectUnique(root, path, out error), "SelectUnique: " + path);
Equal<string>(null, error, "SelectUnique error: " + path);
}
}
public static void Rejected(ControlTreeNode root, string path)
{
string error;
Equal(0, ControlPath.Select(root, path, out error).Count, "Rejected path returned nodes: " + path);
True(!string.IsNullOrWhiteSpace(error), "Rejected path lacks error: " + path);
Equal<ControlTreeNode>(null, ControlPath.SelectUnique(root, path, out error), "Rejected path uniquely resolved: " + path);
True(!string.IsNullOrWhiteSpace(error), "Rejected unique path lacks error: " + path);
}
}
internal sealed class TemporaryDirectory : IDisposable
{
public TemporaryDirectory()
{
DirectoryPath = Path.Combine(Path.GetTempPath(), "pacs-win-uia-regression-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(DirectoryPath);
}
public string DirectoryPath { get; private set; }
public string File(string name) { return Path.Combine(DirectoryPath, name); }
public void Dispose() { Directory.Delete(DirectoryPath, true); }
}
// Every node is an in-memory snapshot: no AutomationElement, HWND lookup, GUI or provider calls.
internal sealed class TreeFixture
{
public const string BreadcrumbLegacy = "[Uia] Custom[PermanentNavigationView] > Group[PermanentNavigationViewBreadcrumbBar] > Button:个性化 > Text:个性化";
public const string MenuLegacy = "[Uia] Window:设置 > Custom[PermanentNavigationView] > Window[PaneRoot] > Pane[MenuItemsScrollViewer] > Group[MenuItemsHost] > ListItem:个性化";
public readonly ControlTreeNode Root;
public readonly ControlTreeNode Navigation;
public readonly ControlTreeNode BreadcrumbText;
public readonly ControlTreeNode MenuItem;
public readonly ControlTreeNode Duplicates;
public readonly ControlTreeNode FirstDuplicate;
public readonly ControlTreeNode SecondDuplicate;
public readonly ControlTreeNode NameWithId;
public readonly ControlTreeNode NameWithoutId;
public readonly ControlTreeNode Nameless;
public readonly ControlTreeNode EscapedName;
public readonly ControlTreeNode EscapedId;
public TreeFixture()
{
Root = Node("Window", "设置", null, "RootClass");
Navigation = Add(Root, Node("Custom", null, "PermanentNavigationView"));
var breadcrumb = Add(Navigation, Node("Group", null, "PermanentNavigationViewBreadcrumbBar"));
BreadcrumbText = Add(Add(breadcrumb, Node("Button", "个性化")), Node("Text", "个性化"));
var paneRoot = Add(Navigation, Node("Window", null, "PaneRoot"));
var scroll = Add(paneRoot, Node("Pane", null, "MenuItemsScrollViewer"));
var host = Add(scroll, Node("Group", null, "MenuItemsHost"));
MenuItem = Add(host, Node("ListItem", "个性化"));
Add(host, Node("ListItem", "系统"));
Duplicates = Add(Root, Node("Group", null, "Duplicates"));
FirstDuplicate = Add(Duplicates, Node("Button", "重复", "same-id", "PrimaryButton"));
Add(Duplicates, Node("Text", "同级异类型"));
Add(Duplicates, Node("Button", "其他", "other-id", "OtherButton"));
SecondDuplicate = Add(Duplicates, Node("Button", "重复", "same-id", "SecondaryButton"));
NameWithId = Add(Duplicates, Node("Button", "混合名称", "has-id"));
NameWithoutId = Add(Duplicates, Node("Button", "混合名称"));
Nameless = Add(Duplicates, Node("Button"));
Add(Duplicates, Node("Button"));
EscapedName = Add(Root, Node("Text", "中文 '单引号' \"双引号\" /斜杠 > 大于 & < [括号]"));
EscapedId = Add(Root, Node("Edit", "输入", "id'\"/ > 中文 & <", "class'\"/ > 中文 & <"));
}
public static ControlTreeNode Node(string type, string name = null, string id = null, string className = null)
{
return new ControlTreeNode
{
Source = ControlTreeSource.Uia,
Uia = new UiaElementInfo { ControlType = "ControlType." + type, Name = name, AutomationId = id, ClassName = className }
};
}
public static ControlTreeNode Add(ControlTreeNode parent, ControlTreeNode child)
{
parent.Children.Add(child);
return child;
}
public static IEnumerable<ControlTreeNode> All(ControlTreeNode root)
{
yield return root;
foreach (ControlTreeNode child in root.Children)
foreach (ControlTreeNode descendant in All(child)) yield return descendant;
}
}
}