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");
}
}
}
}