Files
pacs_win_uia/tests/regression/Program.cs
T
zimoyin 66ab309939 给全部源码补中文注释,并新增代码导览
- 67 个 .cs 文件加中文注释:每个文件顶部说明职责/调用方/线程与节拍/
  已知限制/「要改什么就改哪里」,字段与方法逐条一行说明,复杂逻辑内插关键步骤
- 纯注释改动:脚本校验所有新增行都是注释、无非注释改动(仅 2 个文件的 BOM
  随新头部注释下移,文件仍以 BOM 开头)
- 新增 docs/代码导览.md:模块地图、「我要改 X 看哪个文件」对照表、易踩的坑、
  悬浮条显示规则、测试与验收指引
- 编译通过,回归测试 23/23 通过

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-13 15:44:15 +08:00

215 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// =====================================================================================
// pacs_win_uia 回归测试工程(net48 控制台程序,AssemblyName = RegressionTests)。
//
// 覆盖范围:
// - XPath 定位的解析 / 生成 / 转义 / 歧义与拒绝 ................ PathTests
// - 监控条目与设置的持久化、旧格式迁移、缺省值 ................ PersistenceTests
// - 控件树增量刷新时的节点身份保持 ............................ TreeRefreshTests
// - 悬浮条显示条件的窗口匹配与配置校验 ........................ FloatingConditionsTests
// - 悬浮条进程/类名/标题下拉的原生鼠标与键盘选择 .............. SettingsComboBoxTests
// - 遮挡诊断里“本工具自身窗口不算遮挡” ........................ WindowVisibilityTests
//
// 为什么这样测(重要):
// 用例都在**纯内存快照**(ControlTreeNode)或本进程自建的 WinForms 控件上运行,
// 绝不调用 UIA 去读本进程自己的控件树——UIA 自读会导致进程崩溃(见 MEMORY:
// in-process UIA self-reads segfault)。凡是必须走真实 UIA 的场景,请改用
// tests/uia-fixture 的独立进程做跨进程验证,不要在这里自读。
//
// 新增用例加在哪里:
// 按主题加到对应 *Tests.cs 的 Register(...) 列表里,并在该文件里实现一个
// private static void 方法;断言统一走 Check.*,不要自己 throw 或手写 Console 判断。
//
// Register 模式怎么用:
// 每个测试类提供 static Register(IList<KeyValuePair<string, Action>>),
// 把「用例名 → 委托」成对登记;Main 依次调用它们收集全部用例,再逐个执行并统计。
// =====================================================================================
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
{
// 运行方式:不要在本目录直接 dotnet build(src 的 bin\Debug 可能被正在运行的工具进程锁定)。
// 用一个隔离的 OutputPath 构建,再直接运行生成的 exe:
// dotnet msbuild tests\regression\RegressionTests.csproj -t:Build -p:Configuration=Debug
// -p:OutputPath=<隔离目录>\ -v:minimal
// <隔离目录>\RegressionTests.exe
// 退出码:0 = 全部用例通过;1 = 有任一用例失败(见 Main 末尾的 return)。
[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);
SettingsComboBoxTests.Register(tests);
WindowVisibilityTests.Register(tests);
int passed = 0;
// 逐个执行登记的用例:通过则 PASS 并累加,抛异常则 FAIL 并打印完整异常。
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);
// 退出码约定:0 = 全部通过,1 = 存在失败(供 CI / 脚本判断)。
return passed == tests.Count ? 0 : 1;
}
}
// Check —— 断言助手:所有用例统一用它抛 InvalidOperationException 表示失败,
// 失败信息里带上用例内传入的 message,便于定位。
internal static class Check
{
// 条件必须为真;否则以 message 抛异常。
public static void True(bool condition, string message)
{
if (!condition) throw new InvalidOperationException(message);
}
// 断言值相等(用默认相等比较器);失败时附带 expected/actual。
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");
}
// 断言 path 在 root 上按顺序解析到给定节点;唯一命中时额外校验 SelectUnique 也一致。
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);
}
}
// 断言 path 被拒绝:Select 返回空、SelectUnique 返回 null,且都带非空错误信息。
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);
}
}
// 每个持久化用例独占一个临时目录,Dispose 时递归删除;不碰真实用户配置目录。
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.
//
// TreeFixture —— 供 XPath 用例复用的纯内存控件树夹具,结构:
// Window(设置, RootClass)
// ├─ Custom(PermanentNavigationView)
// │ ├─ Group(...BreadcrumbBar) > Button(个性化) > Text(个性化) → BreadcrumbText
// │ └─ Window(PaneRoot) > Pane(MenuItemsScrollViewer) > Group(MenuItemsHost)
// │ └─ ListItem(个性化) → MenuItem
// ├─ Group(Duplicates):重复 AutomationId、同名异类型、有无 Id、无名字等边界节点
// └─ Text/Edit:中文、单双引号、斜杠、> < & [ ] 等转义样本
// Node(...) 造节点、Add(...) 挂子节点、All(...) 深度优先枚举,供 roundtrip/改名校验遍历全树。
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;
}
}
}