// =====================================================================================
// 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
{
///
/// 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.
///
internal static class WindowVisibilityTests
{
public static void Register(IList> tests)
{
// 防:本工具自身窗口被误判为外部遮挡窗口(或外部窗口被误判为本进程)。
tests.Add(new KeyValuePair("Own-process detection classifies tool windows", OwnProcess));
// 防:遮挡提示把外部窗口也算作本工具窗口,或本工具窗口不被单独说明。
tests.Add(new KeyValuePair("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 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(null, WindowVisibility.DescribeOccluders(null, 0, false), "Empty occluder list");
Check.Equal(null, WindowVisibility.DescribeOccluders(new List(), 0, false), "No occluders");
using (var own = new Form())
{
own.CreateControl();
int pid = System.Diagnostics.Process.GetCurrentProcess().Id;
string ownOnly = WindowVisibility.DescribeOccluders(new List { 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 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 { 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 { foreign[0].Handle }, pid, true);
Check.True(foreignOnly != null && !foreignOnly.Contains("本工具自己的窗口"),
"Foreign occluder was blamed on the tool");
}
}
}
}