// =====================================================================================
// UiaFixture —— 跨进程验证用的真实 WinForms 夹具(单独的可执行程序)。
//
// 为什么必须是独立进程:本工具靠 UIA 读取“别的进程”的控件树,而 UIA 在**同一进程内**
// 自读自己的控件树会崩溃(见 MEMORY:in-process UIA self-reads segfault)。
// 因此需要一个真正独立的进程暴露一棵已知且稳定的控件树,让工具跨进程去读、去拾取、
// 去按 path 定位——这样测的才是生产路径。夹具自身不做任何 UIA 调用。
//
// 这棵树的控件类型/名称/AutomationId 都固定,且窗口标题与类名在多次运行间保持不变,
// 所以保存的监控条目、悬浮条显示条件可以长期复用它。
//
// 打开方式:tests\run-fixture.cmd(或用 Rider/VS 直接跑本项目)→ 工具里
// 刷新窗口 → 选中「UIA 测试夹具」→ 检查选中窗口;验证清单见 tests\README.md。
// =====================================================================================
using System;
using System.Drawing;
using System.Windows.Forms;
namespace UiaFixture
{
///
/// A deliberately ordinary window for exercising the tool: every kind of control the tool has to
/// read (button, text box, checkbox, combo, radio, tree, list, grid, tabs) in one window, with
/// stable names and AutomationIds so a saved path keeps resolving.
///
/// This exists because the PACS/OCS clients either expose nothing (CEF shells) or expose a large
/// tree nobody wants to hand-verify against; here the expected answer is known.
///
/// Run it, then in the tool: 刷新窗口 → select "UIA 测试夹具" → 检查选中窗口. The window title and
/// class stay the same between runs, so saved monitor entries and floating-bar conditions can be
/// built against it.
///
public sealed class FixtureForm : Form
{
private readonly Label _status;
private readonly TextBox _input;
private readonly RichTextBox _rich;
private readonly CheckBox _check;
private readonly ComboBox _combo;
private readonly TreeView _tree;
private readonly ListView _list;
private readonly DataGridView _grid;
/// 启动时选中列表的第几行(0-based,-1 表示不选)。给自动化验证用。
public int SelectRowOnStartup { get; set; }
public FixtureForm()
{
Text = "UIA 测试夹具";
Name = "FixtureForm";
StartPosition = FormStartPosition.Manual;
Location = new Point(60, 60);
Size = new Size(760, 620);
MinimumSize = new Size(600, 480);
var tabs = new TabControl { Dock = DockStyle.Fill, Name = "FixtureTabs" };
// ---- page 1: the everyday controls
var page = new TabPage("控件") { Name = "ControlsPage" };
var panel = new Panel { Dock = DockStyle.Fill, Padding = new Padding(10) };
var button = new Button
{
Text = "测试按钮",
Name = "FixtureButton",
AutoSize = true,
Location = new Point(12, 12)
};
button.Click += delegate { SetStatus("按钮被点击:" + DateTime.Now.ToString("HH:mm:ss")); };
_input = new TextBox
{
Name = "FixtureTextBox",
Text = "可编辑文本",
Location = new Point(140, 14),
Width = 220
};
_input.TextChanged += delegate { SetStatus("文本框内容已改为:" + _input.Text); };
// 多行富文本:内容较长,用来验证"内容在 Name/Value/Text 里"的取值与拾取定位。
_rich = new RichTextBox
{
Name = "FixtureRichText",
Text = "富文本第一行" + Environment.NewLine + "富文本第二行",
Location = new Point(380, 14),
Size = new Size(240, 60)
};
_check = new CheckBox
{
Text = "启用选项",
Name = "FixtureCheckBox",
Location = new Point(12, 50),
AutoSize = true
};
_check.CheckedChanged += delegate { SetStatus("复选框:" + (_check.Checked ? "选中" : "未选中")); };
var radioA = new RadioButton { Text = "选项 A", Name = "FixtureRadioA", Location = new Point(140, 50), AutoSize = true, Checked = true };
var radioB = new RadioButton { Text = "选项 B", Name = "FixtureRadioB", Location = new Point(240, 50), AutoSize = true };
radioA.CheckedChanged += delegate { SetStatus("单选:A"); };
radioB.CheckedChanged += delegate { SetStatus("单选:B"); };
_combo = new ComboBox
{
Name = "FixtureComboBox",
DropDownStyle = ComboBoxStyle.DropDownList,
Location = new Point(12, 80),
Width = 200
};
_combo.Items.AddRange(new object[] { "第一项", "第二项", "第三项" });
_combo.SelectedIndex = 0;
_combo.SelectedIndexChanged += delegate { SetStatus("下拉选择:" + _combo.SelectedItem); };
var listLabel = new Label { Text = "列表(两列)", AutoSize = true, Location = new Point(12, 116) };
_list = new ListView
{
Name = "FixtureListView",
View = View.Details,
FullRowSelect = true,
Location = new Point(12, 138),
Size = new Size(320, 150)
};
_list.Columns.Add("姓名", 120);
_list.Columns.Add("编号", 100);
for (int index = 1; index <= 3; index++)
{
_list.Items.Add(new ListViewItem(new[] { "项目 " + index, "ID" + index }));
}
// 让测试能预先选中某一行(例如验证 XPath 的 @IsSelected / @IsFocused)。
SelectRowOnStartup = 0;
_tree = new TreeView
{
Name = "FixtureTreeView",
Location = new Point(348, 138),
Size = new Size(360, 150)
};
TreeNode root = _tree.Nodes.Add("根节点");
root.Nodes.Add("子节点 1");
root.Nodes.Add("子节点 2");
root.ExpandAll();
_status = new Label
{
Name = "FixtureStatusLabel",
Text = "还没有操作。",
AutoSize = false,
Height = 22,
Location = new Point(12, 296),
Width = 700,
ForeColor = Color.DarkSlateGray
};
panel.Controls.Add(button);
panel.Controls.Add(_input);
panel.Controls.Add(_rich);
panel.Controls.Add(_check);
panel.Controls.Add(radioA);
panel.Controls.Add(radioB);
panel.Controls.Add(_combo);
panel.Controls.Add(listLabel);
panel.Controls.Add(_list);
panel.Controls.Add(_tree);
panel.Controls.Add(_status);
page.Controls.Add(panel);
// ---- page 2: a grid, for the list reader
var gridPage = new TabPage("表格") { Name = "GridPage" };
_grid = new DataGridView
{
Name = "FixtureDataGrid",
Dock = DockStyle.Fill,
AllowUserToAddRows = false,
ReadOnly = true,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
};
_grid.Columns.Add("序号", "序号");
_grid.Columns.Add("内容", "内容");
_grid.Columns.Add("状态", "状态");
for (int index = 1; index <= 5; index++)
{
_grid.Rows.Add(index.ToString(), "第 " + index + " 行内容", index % 2 == 0 ? "已完成" : "待处理");
}
gridPage.Controls.Add(_grid);
// ---- page 3: a nested container, for "the list is inside the picked element"
var nestedPage = new TabPage("嵌套") { Name = "NestedPage" };
var outer = new Panel { Dock = DockStyle.Fill, Padding = new Padding(8), Name = "NestedOuter" };
var middle = new Panel { Dock = DockStyle.Fill, Padding = new Padding(8), Name = "NestedMiddle", BackColor = Color.WhiteSmoke };
var innerList = new ListBox { Dock = DockStyle.Fill, Name = "NestedListBox" };
for (int index = 1; index <= 4; index++)
{
innerList.Items.Add("嵌套列表项 " + index);
}
middle.Controls.Add(innerList);
outer.Controls.Add(middle);
nestedPage.Controls.Add(outer);
tabs.TabPages.Add(page);
tabs.TabPages.Add(gridPage);
tabs.TabPages.Add(nestedPage);
Controls.Add(tabs);
Shown += delegate
{
if (SelectRowOnStartup < 0 || SelectRowOnStartup >= _list.Items.Count)
{
return;
}
_list.Items[SelectRowOnStartup].Selected = true;
_list.Items[SelectRowOnStartup].Focused = true;
_list.Select();
_list.EnsureVisible(SelectRowOnStartup);
};
// A second top-level window so "which window" itself can be tested.
var secondary = new Form
{
Text = "UIA 测试夹具 - 第二个窗口",
Name = "FixtureSecondWindow",
Owner = this,
StartPosition = FormStartPosition.Manual,
Location = new Point(840, 60),
Size = new Size(360, 220)
};
var secondaryButton = new Button
{
Text = "第二个窗口的按钮",
Name = "FixtureSecondButton",
Location = new Point(20, 24),
AutoSize = true
};
secondaryButton.Click += delegate { SetStatus("第二个窗口的按钮被点击。"); };
secondary.Controls.Add(secondaryButton);
secondary.Shown += delegate { secondary.Location = new Point(Location.X + Width + 20, Location.Y); };
AddOwnedForm(secondary);
}
private void SetStatus(string text)
{
_status.Text = DateTime.Now.ToString("HH:mm:ss") + " " + text;
}
}
internal static class Program
{
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new FixtureForm();
// UiaFixture.exe --select-row 2 → 启动时选中列表第 2 行(1-based),
// 用于验证 XPath 的 @IsSelected/@IsFocused 与"选中行"取值。
if (args != null && args.Length >= 2 && string.Equals(args[0], "--select-row", StringComparison.Ordinal))
{
int oneBased;
if (int.TryParse(args[1], out oneBased))
{
form.SelectRowOnStartup = oneBased - 1;
}
}
Application.Run(form);
}
}
}