Files
pacs_win_uia/tests/regression/WindowVisibilityTests.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

95 lines
5.3 KiB
C#

// =====================================================================================
// WindowVisibilityTests —— 悬浮条遮挡诊断的窗口归属判断回归。
//
// 覆盖范围:WindowVisibility.IsOwnProcessWindow 对零句柄/本进程窗体/外部窗口的分类,
// 以及 DescribeOccluders 生成的遮挡说明——本工具自己的窗口必须被单列为“本工具窗口/
// 设置页自检”,外部窗口则只能被称作普通遮挡,绝不能反过来把遮挡归咎于本工具。
//
// 为什么这样测:只用本测试进程自建的 WinForms 窗体句柄与手工构造的 IntPtr 列表;
// WindowEnumerationResult 天然排除本进程,所以它返回的窗口一定属于外部进程。
// 不调用 UIA、也不操作真实目标窗口。外部窗口很可能不存在(由外部环境决定),
// 因此相关断言在无外部窗口时会打印提示并跳过,属于预期内的“部分跳过”。
//
// 新增用例:加到 Register(...) 列表并实现 private static void 方法,断言走 Check.*。
// =====================================================================================
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using pacs_win_uia.Models;
using pacs_win_uia.Services;
namespace PacsWinUia.RegressionTests
{
/// <summary>
/// Coverage for the occlusion diagnostic used by 设置 → 悬浮按钮 → 检测显示条件.
/// The tool's own windows must not be reported as occluders there (the settings dialog itself
/// covers the target while the operator is configuring), while foreign windows still must be.
/// No UIA and no target manipulation: real Win32 windows of this test process only.
/// </summary>
internal static class WindowVisibilityTests
{
public static void Register(IList<KeyValuePair<string, Action>> tests)
{
// 防:本工具自身窗口被误判为外部遮挡窗口(或外部窗口被误判为本进程)。
tests.Add(new KeyValuePair<string, Action>("Own-process detection classifies tool windows", OwnProcess));
// 防:遮挡提示把外部窗口也算作本工具窗口,或本工具窗口不被单独说明。
tests.Add(new KeyValuePair<string, Action>("Occluder message names the blocker and never blames the tool wrongly", OccluderMessages));
}
private static void OwnProcess()
{
Check.True(!WindowVisibility.IsOwnProcessWindow(IntPtr.Zero), "Zero handle is not an own window");
using (var form = new Form())
{
form.CreateControl();
IntPtr handle = form.Handle;
Check.True(handle != IntPtr.Zero, "Test form has no handle");
Check.True(WindowVisibility.IsOwnProcessWindow(handle), "Own form was not recognised as own process");
}
// WindowEnumerationResult always excludes this process, so anything it returns is foreign.
IList<NativeWindowInfo> foreign = new NativeWindowService().EnumerateTopLevelWindows(false);
if (foreign.Count == 0)
{
Console.WriteLine(" (no foreign window available on this desktop; foreign half skipped)");
return;
}
Check.True(!WindowVisibility.IsOwnProcessWindow(foreign[0].Handle),
"Foreign window was classified as own process");
}
private static void OccluderMessages()
{
Check.Equal<string>(null, WindowVisibility.DescribeOccluders(null, 0, false), "Empty occluder list");
Check.Equal<string>(null, WindowVisibility.DescribeOccluders(new List<IntPtr>(), 0, false), "No occluders");
using (var own = new Form())
{
own.CreateControl();
int pid = System.Diagnostics.Process.GetCurrentProcess().Id;
string ownOnly = WindowVisibility.DescribeOccluders(new List<IntPtr> { own.Handle }, pid, true);
Check.True(ownOnly != null && ownOnly.Contains("本工具"), "Own-window occluder not named as tool window");
Check.True(ownOnly.Contains("设置页自检"), "Own-window occluder message does not explain the diagnostic case");
IList<NativeWindowInfo> foreign = new NativeWindowService().EnumerateTopLevelWindows(false);
if (foreign.Count == 0)
{
Console.WriteLine(" (no foreign window available on this desktop; mixed half skipped)");
return;
}
string mixed = WindowVisibility.DescribeOccluders(
new List<IntPtr> { own.Handle, foreign[0].Handle }, pid, false);
Check.True(mixed != null && mixed.Contains("遮挡窗口"), "Mixed occluder list missing header");
Check.True(mixed.Contains("其中 1 个是本工具窗口"), "Mixed occluder list did not count own windows");
string foreignOnly = WindowVisibility.DescribeOccluders(
new List<IntPtr> { foreign[0].Handle }, pid, true);
Check.True(foreignOnly != null && !foreignOnly.Contains("本工具自己的窗口"),
"Foreign occluder was blamed on the tool");
}
}
}
}