Files
pacs_win_uia/tests/regression/ValueKeyTests.cs
T
zimoyin ebbf9e1335 选中行/焦点行语法、列表信息键、拾取层级框、控件属性补全
1) 新增「选中行/焦点行」语法,控件监控新增列表信息
   - XPath 快照新增状态属性:@IsSelected / @IsFocused / @IsOffscreen / @IsEnabled
     例如 /Window[1]//DataGrid[@AutomationId='x']/DataItem[@IsSelected='true']
   - IsSelected 随控件树一起从缓存读(SelectionItemPattern);对只在容器上暴露
     SelectionPattern 的提供者(实测 WinForms ListView/DataGridView),树读完后
     由容器反推每个子项的选中状态,@IsSelected 与「选中状态」取值因此可用
   - 新增取值键:列表行数 / 列表列数 / 列表选中项数(Grid → Table → Selection)
   - 控件属性新增「列表概况」(行×列 + 选中项数 + 首项文本)

2) 拾取时显示层级:蓝框=光标所在窗口,黄框=向上遍历的控件链
   - PointPickResult 增加 HierarchyFrames;TargetProbeService 填蓝色窗口层 +
     UiAutomationService.DescribeAncestorFrames 产出的黄色控件链(最多 6 层)
   - 新增 UI/PickHierarchyOverlayForm:跟随锚点窗口、只读 GetWindowRect、
     最小化/关闭即收起、置顶不抢焦点、点击穿透
   - CrosshairPicker 预览改用层级框;只有 Win32/MSAA 信息时退回原来的单框高亮

3) 控件属性按 UIA/Win32 分类补全(没有的就新增)
   - UIA:父元素、RawView/ControlView/ContentView 子元素数、RangeValue、
     Toggle 状态、选中情况、列表概况(全部按需读取,选中节点时才跨进程)
   - Win32:样式 GWL_STYLE、扩展样式 GWL_EXSTYLE、启用、最大化、层级关系
     (父窗口 / 直接子窗口数 / 同类序号)
   - GetWindowStyle 原本读的其实是 GWL_EXSTYLE,已正名为 GetWindowExStyle,
     GWL_STYLE 用新方法读取

实测(跨进程,目标为夹具/记事本):
- 夹具列表选中第 2 行:DataItem[2] IsSelected=True;@IsSelected='true' 唯一命中并
  取到「项目 2」;列表行数=3、列表列数=2、列表选中项数=1
- 拾取夹具列表行:7 层框(1 蓝窗口 + 6 黄控件链),覆盖层实际绘制并截图确认
- 记事本文档:控件属性显示 文本内容="agfa\r\raaaaa"(Name 是「文本编辑器」)

回归 29/29 通过
2026-10-13 19:19:16 +08:00

165 lines
9.0 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.
// =====================================================================================
// ValueKeyTests —— 取值键(尤其是新增的跨来源键「文本内容」)的回归。
//
// 覆盖范围:
// · ControlValueKeys 必须暴露「文本内容」,并且它是 UIA/Auto 的第一个推荐键;
// · ControlValueReader 对「文本内容」的降级顺序:Value → Text → Name
// (快照没有可用的 UIA 元素时,也应该退回 Name,而不是直接报错——很多客户端
// 就是把输入框/富文本的内容放在 Name 里);
// · 新增监控项/拾取控件时默认保存的 ValueKey 是「文本内容」。
//
// 为什么这样测:全部用内存里的 UiaElementInfo 快照 + 内存控件树,不调用 UIA、
// 不启动目标程序,结果稳定(真正跨进程的取值验证见 tests/uia-fixture 手工清单)。
//
// 新增用例:加到 Register(...) 列表并实现 private static void 方法,断言走 Check.*。
// =====================================================================================
using System;
using System.Collections.Generic;
using pacs_win_uia.Models;
using pacs_win_uia.Services;
namespace PacsWinUia.RegressionTests
{
internal static class ValueKeyTests
{
public static void Register(IList<KeyValuePair<string, Action>> tests)
{
// 防:文本内容键被移除/排到后面,导致监控列表默认又去读 Name。
tests.Add(new KeyValuePair<string, Action>("Text content key is offered first for UIA", TextContentKeyOffered));
// 防:内容在 Name 里的客户端(WinForms 输入框、自绘界面)读不到值。
tests.Add(new KeyValuePair<string, Action>("Text content falls back to the snapshot Name", TextContentFallsBackToName));
// 防:新增控件默认 ValueKey 回到 Name;富文本这类控件会读到 "RichEdit Control"。
tests.Add(new KeyValuePair<string, Action>("New monitor entries default to the text content key", BuilderDefaultsToTextContent));
// 防:XPath 状态属性(选中行/焦点行)消失或写错,导致 @IsSelected/@IsFocused 语法失效。
tests.Add(new KeyValuePair<string, Action>("XPath can select the selected and focused row", RowStateAttributes));
// 防:列表信息键被移除,监控里没法盯住"这张表几行、选中几行"。
tests.Add(new KeyValuePair<string, Action>("List information keys are offered for UIA", ListKeysOffered));
}
// XPath 属性 @IsSelected / @IsFocused / @IsOffscreen / @IsEnabled:
// 源图是"选中第 2 行"的列表,应当能唯一命中它。
private static void RowStateAttributes()
{
ControlTreeNode root = Row("Window", "列表窗口");
ControlTreeNode grid = Row("DataGrid", "列表", "FixtureListView");
root.Children.Add(grid);
grid.Children.Add(Row("DataItem", "项目 1", null, selected: false, focused: false));
grid.Children.Add(Row("DataItem", "项目 2", null, selected: true, focused: true));
grid.Children.Add(Row("DataItem", "项目 3", null, selected: false, focused: false));
ControlPath.PreparePaths(root);
Check.Matches(root, "//DataItem[@IsSelected='true']", grid.Children[1]);
Check.Matches(root, "//DataItem[@IsFocused='true']", grid.Children[1]);
Check.Matches(root, "//DataItem[@IsSelected='false']", grid.Children[0], grid.Children[2]);
Check.Matches(root, "//DataItem[@IsEnabled='true' and @IsOffscreen='false']",
grid.Children[0], grid.Children[1], grid.Children[2]);
Check.True(ControlValueReader.TryRead(grid.Children[1], ControlValueKeys.TextContent,
new ListReadOptions(), null, out string value, out string error) == false || value != null,
"选中行取值不应抛异常");
}
private static ControlTreeNode Row(string type, string name, string automationId = null,
bool? selected = null, bool? focused = null)
{
return new ControlTreeNode
{
Source = ControlTreeSource.Uia,
Uia = new UiaElementInfo
{
ControlType = "ControlType." + type,
Name = name,
AutomationId = automationId,
IsAvailable = true,
IsSelected = selected ?? (bool?)null,
HasKeyboardFocus = focused ?? (bool?)null,
IsOffscreen = false,
IsEnabled = true
}
};
}
private static void ListKeysOffered()
{
foreach (string key in new[]
{
ControlValueKeys.ListRowCount, ControlValueKeys.ListColumnCount, ControlValueKeys.ListSelectedCount
})
{
Check.True(ControlValueKeys.For(ControlSourcePreference.Uia).Contains(key),
"UIA keys missing " + key);
}
Check.Equal("列表行数", ControlValueKeys.ListRowCount, "Row count key name");
Check.Equal("列表选中项数", ControlValueKeys.ListSelectedCount, "Selected count key name");
}
private static void TextContentKeyOffered()
{
Check.Equal("文本内容", ControlValueKeys.TextContent, "Text content key name");
Check.Equal(ControlValueKeys.TextContent, ControlValueKeys.Uia[0], "Text content should be the first UIA key");
Check.True(ControlValueKeys.For(ControlSourcePreference.Uia).Contains(ControlValueKeys.TextContent),
"UIA keys missing the text content key");
Check.True(ControlValueKeys.For(ControlSourcePreference.Auto).Contains(ControlValueKeys.TextContent),
"Auto keys missing the text content key");
Check.True(ControlValueKeys.ForSource(ControlTreeSource.Uia).Contains(ControlValueKeys.TextContent),
"Source-based keys missing the text content key");
}
private static void TextContentFallsBackToName()
{
// 没有 Element(快照已脱离元素):模式调用不可用,必须退回 Name。
var node = new ControlTreeNode
{
Source = ControlTreeSource.Uia,
Uia = new UiaElementInfo { ControlType = "ControlType.Edit", Name = "内容在 Name 里", IsAvailable = true }
};
string value;
string error;
Check.True(ControlValueReader.TryRead(node, ControlValueKeys.TextContent,
new ListReadOptions(), null, out value, out error), "Text content failed without an element: " + error);
Check.Equal("内容在 Name 里", value, "Text content did not fall back to Name");
// Name 也为空 → 明确失败,并说明 Value/Text/Name 都取不到。
var empty = new ControlTreeNode
{
Source = ControlTreeSource.Uia,
Uia = new UiaElementInfo { ControlType = "ControlType.Edit", IsAvailable = true }
};
Check.True(!ControlValueReader.TryRead(empty, ControlValueKeys.TextContent,
new ListReadOptions(), null, out value, out error), "Empty control should fail");
Check.True(error != null && error.Contains("Name"),
"Failure message should mention the inspected sources: " + error);
}
private static void BuilderDefaultsToTextContent()
{
var root = new ControlTreeNode
{
Source = ControlTreeSource.Uia,
Uia = new UiaElementInfo { ControlType = "ControlType.Window", Name = "窗口", ClassName = "Host", IsAvailable = true }
};
var rich = new ControlTreeNode
{
Source = ControlTreeSource.Uia,
// 富文本的典型样子:Name 是通用标题,真正的内容只在 Value/Text 模式里。
Uia = new UiaElementInfo
{
ControlType = "ControlType.Document", Name = "RichEdit Control",
AutomationId = "FixtureRichText", ClassName = "RICHEDIT50W", IsAvailable = true
}
};
root.Children.Add(rich);
ControlPath.PreparePaths(root);
var window = new NativeWindowInfo(IntPtr.Zero) { ProcessName = "fixture", ClassName = "Host", Title = "夹具" };
ControlLocatorBuildResult built = new ControlLocatorBuilder().BuildFromTree(root, rich, window);
Check.Equal(ControlValueKeys.TextContent, built.ValueKey, "Builder default value key must be the text content key");
Check.True(built.IsComplete, "Builder path incomplete: " + built.Note);
Check.True(built.DisplayPath.IndexOf("@AutomationId='FixtureRichText'", StringComparison.Ordinal) >= 0,
"Builder path should identify the text control: " + built.DisplayPath);
}
}
}