Files
pacs_win_uia/tests/regression/ValueKeyTests.cs
T
zimoyin 170790ff12 新增「选中行文本」取值键;点击控件框必定同步控件属性
- ControlValueKeys 新增「选中行文本」:在列表容器上直接读当前选中行的文字
  (SelectionPattern 第一个选中项 → Value → Text → Name);没有选中行时明确报错
- 控件框点选回调:无论该节点是否在当前控件树里,都更新控件属性(SelectedObject +
  ShowNodeProperties + 下方 Path 横条),并在状态栏说明是否同时选中了树节点

实测(夹具列表,启动时选中第 2 行):列表行数=3、列数=2、选中项数=1、选中行文本=项目 2
回归 29/29 通过
2026-10-13 19:28:07 +08:00

167 lines
9.2 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, ControlValueKeys.SelectedRowText
})
{
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");
Check.Equal("选中行文本", ControlValueKeys.SelectedRowText, "Selected row text 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);
}
}
}