新增跨来源取值键「文本内容」,修好富文本/输入框读不到内容

问题:富文本(RichTextBox、Chromium/CEF、UWP 文本框)的 UIA Name 往往是通用标题
(实测夹具里是 "RichEdit Control"),真正的内容只在 ValuePattern/TextPattern 里;
而新增监控项时默认 ValueKey 是 Name,于是读回来的是标题而不是内容。

- ControlValueKeys 新增跨来源键「文本内容」:UIA 按 Value → Text → Name 顺序取,
  MSAA = accValue → accName,Win32 = 窗口标题;三种都取不到才报错并说明各自原因
- 新增/拾取控件时默认 ValueKey 改为「文本内容」(旧条目不受影响,仍按各自保存的键读)
- 「文本内容」加进三个来源的可选键列表,监控列表下拉里能直接选
- 夹具新增多行富文本 FixtureRichText(内容只在 Value/Text 里)
- 回归新增 ValueKeyTests(键表、Name 兜底、默认键),共 27/27 通过

实测(跨进程,目标为独立进程里的夹具窗口):
- 文本框 文本内容="可编辑文本";富文本 文本内容="富文本第一行\r富文本第二行\r";
  按钮(无 Value/Text 模式)自动退回 Name="测试按钮"
- 拾取点在两个控件上都精确命中 Edit / Document,且能在窗口树里找到同一节点
- 无界面读取端到端:2 条配置(//Document[...]、//Edit[...] + 文本内容)全部命中并取到值
This commit is contained in:
zimoyin
2026-10-13 16:22:03 +08:00
parent 44fb305dc4
commit 529f7910a1
9 changed files with 187 additions and 6 deletions
+102
View File
@@ -0,0 +1,102 @@
// =====================================================================================
// 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));
}
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);
}
}
}