Skip to content

Commit 1459e5f

Browse files
github-actions[bot]kastweyShyam-GuptaCopilot
authored
[release/10.0] Fix inverted NoClientNotifications check in Control.AccessibilityNotifyClients (#15216)
* Fix inverted NoClientNotifications check in Control.AccessibilityNotifyClients (#15177) The rename of LocalAppContextSwitches to AppContextSwitches in #12840 dropped the negation on this line (and only on this one of the seven that use the switch), so controls only call NotifyWinEvent when the app has asked for NO client notifications, that is, never by default. MSAA clients (JAWS among them) stopped receiving the WinEvents raised through this method: menu popup start/end, focus of ToolStrip items, state and selection changes, etc. .NET 9: if (IsHandleCreated && !LocalAppContextSwitches.NoClientNotifications) .NET 10: if (IsHandleCreated && AppContextSwitches.NoClientNotifications) Adds tests that listen to the WinEvent; the existing ones only checked that the call did not throw. * Fix comment formatting in ControlTests.AccessibilityNotifyClients.cs Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Juanjo <juanjo@jmontiel.es> Co-authored-by: Shyam Gupta <shgu@microsoft.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent cf3f34b commit 1459e5f

2 files changed

Lines changed: 109 additions & 1 deletion

File tree

‎src/System.Windows.Forms/System/Windows/Forms/Control.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4226,7 +4226,7 @@ protected internal void AccessibilityNotifyClients(AccessibleEvents accEvent, in
42264226
[EditorBrowsable(EditorBrowsableState.Advanced)]
42274227
protected void AccessibilityNotifyClients(AccessibleEvents accEvent, int objectID, int childID)
42284228
{
4229-
if (IsHandleCreated && AppContextSwitches.NoClientNotifications)
4229+
if (IsHandleCreated && !AppContextSwitches.NoClientNotifications)
42304230
{
42314231
PInvoke.NotifyWinEvent((uint)accEvent, this, objectID, childID + 1);
42324232
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Runtime.CompilerServices;
5+
using System.Runtime.InteropServices;
6+
using Windows.Win32.UI.Accessibility;
7+
8+
namespace System.Windows.Forms.Tests;
9+
10+
public partial class ControlTests
11+
{
12+
[WinFormsFact]
13+
public void Control_AccessibilityNotifyClients_WithHandle_RaisesWinEvent()
14+
{
15+
using NoClientNotificationsScope scope = new(enable: false);
16+
using SubControl control = new();
17+
Assert.NotEqual(IntPtr.Zero, control.Handle);
18+
19+
using WinEventListener listener = new(AccessibleEvents.Focus);
20+
control.AccessibilityNotifyClients(AccessibleEvents.Focus, 2);
21+
22+
WinEventListener.WinEvent winEvent = Assert.Single(listener.EventsOf(control));
23+
Assert.Equal((uint)AccessibleEvents.Focus, winEvent.Event);
24+
Assert.Equal((int)OBJECT_IDENTIFIER.OBJID_CLIENT, winEvent.ObjectId);
25+
26+
// MSAA child ids are 1-based.
27+
Assert.Equal(3, winEvent.ChildId);
28+
}
29+
30+
[WinFormsFact]
31+
public void Control_AccessibilityNotifyClients_ObjectId_WithHandle_RaisesWinEvent()
32+
{
33+
using NoClientNotificationsScope scope = new(enable: false);
34+
using SubControl control = new();
35+
Assert.NotEqual(IntPtr.Zero, control.Handle);
36+
37+
using WinEventListener listener = new(AccessibleEvents.SystemMenuStart);
38+
control.AccessibilityNotifyClients(AccessibleEvents.SystemMenuStart, (int)OBJECT_IDENTIFIER.OBJID_MENU, -1);
39+
40+
WinEventListener.WinEvent winEvent = Assert.Single(listener.EventsOf(control));
41+
Assert.Equal((int)OBJECT_IDENTIFIER.OBJID_MENU, winEvent.ObjectId);
42+
Assert.Equal(0, winEvent.ChildId);
43+
}
44+
45+
[WinFormsFact]
46+
public void Control_AccessibilityNotifyClients_NoClientNotifications_DoesNotRaiseWinEvent()
47+
{
48+
using NoClientNotificationsScope scope = new(enable: true);
49+
using SubControl control = new();
50+
Assert.NotEqual(IntPtr.Zero, control.Handle);
51+
52+
using WinEventListener listener = new(AccessibleEvents.Focus);
53+
control.AccessibilityNotifyClients(AccessibleEvents.Focus, 2);
54+
55+
Assert.Empty(listener.EventsOf(control));
56+
}
57+
58+
/// <summary>
59+
/// Listens, on the current thread, to the WinEvents of one kind raised by this process.
60+
/// The hook is in-context, so the callback runs synchronously inside NotifyWinEvent.
61+
/// </summary>
62+
private sealed unsafe class WinEventListener : IDisposable
63+
{
64+
public readonly record struct WinEvent(uint Event, HWND Hwnd, int ObjectId, int ChildId);
65+
66+
// The hook is installed for the current thread only, and WinForms tests do not run in parallel.
67+
private static List<WinEvent>? s_events;
68+
69+
private readonly HWINEVENTHOOK _hook;
70+
71+
public WinEventListener(AccessibleEvents accEvent)
72+
{
73+
s_events = [];
74+
_hook = PInvoke.SetWinEventHook(
75+
(uint)accEvent,
76+
(uint)accEvent,
77+
PInvoke.GetModuleHandle((PCWSTR)null),
78+
&WinEventProc,
79+
(uint)Environment.ProcessId,
80+
PInvokeCore.GetCurrentThreadId(),
81+
PInvoke.WINEVENT_INCONTEXT);
82+
83+
Assert.False(_hook.IsNull);
84+
}
85+
86+
public IEnumerable<WinEvent> EventsOf(Control control)
87+
{
88+
HWND hwnd = control.HWND;
89+
return s_events!.Where(e => e.Hwnd == hwnd);
90+
}
91+
92+
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
93+
private static void WinEventProc(
94+
HWINEVENTHOOK hook,
95+
uint @event,
96+
HWND hwnd,
97+
int idObject,
98+
int idChild,
99+
uint idEventThread,
100+
uint dwmsEventTime) => s_events?.Add(new(@event, hwnd, idObject, idChild));
101+
102+
public void Dispose()
103+
{
104+
PInvoke.UnhookWinEvent(_hook);
105+
s_events = null;
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)