From da3321198230a0bd6932acabb53a4d2220a505d7 Mon Sep 17 00:00:00 2001 From: Devendar Reddy Adulla Date: Thu, 15 Apr 2021 14:05:51 -0700 Subject: [PATCH 1/8] Seems this changes was missed from Day 1 and a regression from .NET framework. Without these changes, on PMv2 applications, We might be ending up with wrong Dpiawareness than the requested for windows and some times, might endup failing to parent because of inconsitance DpIcontexts. https://github.com/dotnet/winforms/pull/2262 addressed for catching the failure but still applications may be still creating wrong DPIawareness windows. --- .../src/System/Windows/Forms/Application.ThreadContext.cs | 6 +++--- .../src/System/Windows/Forms/Application.cs | 4 ---- src/System.Windows.Forms/src/System/Windows/Forms/AxHost.cs | 2 +- .../src/System/Windows/Forms/Control.cs | 6 +++--- .../src/System/Windows/Forms/WebBrowserBase.cs | 2 +- 5 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Application.ThreadContext.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Application.ThreadContext.cs index 8282b1666d4..8ead9c08653 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Application.ThreadContext.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Application.ThreadContext.cs @@ -309,10 +309,10 @@ internal ParkingWindow GetParkingWindowForContext(IntPtr context) // Legacy OS/target framework scenario where ControlDpiContext is set to DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_UNSPECIFIED // because of 'ThreadContextDpiAwareness' API unavailability or this feature is not enabled. - - if (!DpiHelper.IsScalingRequirementMet || User32.AreDpiAwarenessContextsEqual(context, User32.UNSPECIFIED_DPI_AWARENESS_CONTEXT)) + if (_parkingWindows.Count == 1 + && (!DpiHelper.IsScalingRequirementMet + || User32.AreDpiAwarenessContextsEqual(context, User32.UNSPECIFIED_DPI_AWARENESS_CONTEXT))) { - Debug.Assert(_parkingWindows.Count == 1, "parkingWindows count can not be > 1 for legacy OS/target framework versions"); return _parkingWindows[0]; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs index f780907d4f4..6292caa9bf5 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs @@ -1063,8 +1063,6 @@ private static void RaiseThreadExit() } } - internal static void ParkHandle(HandleRef handle) => ParkHandle(handle, User32.UNSPECIFIED_DPI_AWARENESS_CONTEXT); - /// /// "Parks" the given HWND to a temporary HWND. This allows WS_CHILD windows to /// be parked. @@ -1081,8 +1079,6 @@ internal static void ParkHandle(HandleRef handle, IntPtr dpiAwarenessContext) } } - internal static void ParkHandle(CreateParams cp) => ParkHandle(cp, User32.UNSPECIFIED_DPI_AWARENESS_CONTEXT); - /// /// Park control handle on a parkingwindow that has matching DpiAwareness. /// diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/AxHost.cs b/src/System.Windows.Forms/src/System/Windows/Forms/AxHost.cs index e881ce62587..6b253d72958 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/AxHost.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/AxHost.cs @@ -3502,7 +3502,7 @@ protected unsafe override void WndProc(ref Message m) IntPtr hwnd = IntPtr.Zero; if (ipo.GetWindow(&hwnd).Succeeded()) { - Application.ParkHandle(new HandleRef(ipo, hwnd)); + Application.ParkHandle(new HandleRef(ipo, hwnd), DpiAwarenessContext); } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs index 5a81f4ccf70..d44283e7f87 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs @@ -4978,7 +4978,7 @@ protected virtual void CreateHandle() if (cp.Parent == IntPtr.Zero && (cp.Style & (int)User32.WS.CHILD) != 0) { Debug.Assert((cp.ExStyle & (int)User32.WS_EX.MDICHILD) == 0, "Can't put MDI child forms on the parking form"); - Application.ParkHandle(cp); + Application.ParkHandle(cp, DpiAwarenessContext); } _window.CreateHandle(cp); @@ -7468,7 +7468,7 @@ internal virtual void OnParentHandleRecreating() // use SetParent directly so as to not raise ParentChanged events if (IsHandleCreated) { - Application.ParkHandle(new HandleRef(this, Handle)); + Application.ParkHandle(new HandleRef(this, Handle), DpiAwarenessContext); } } @@ -10797,7 +10797,7 @@ private void SetParentHandle(IntPtr value) { if (value == IntPtr.Zero) { - Application.ParkHandle(new HandleRef(_window, Handle)); + Application.ParkHandle(new HandleRef(_window, Handle), DpiAwarenessContext); UpdateRoot(); } else diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserBase.cs b/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserBase.cs index fc2d3ae1cb7..2d3d6997d1d 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserBase.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserBase.cs @@ -454,7 +454,7 @@ protected unsafe override void WndProc(ref Message m) IntPtr hwndInPlaceObject = IntPtr.Zero; if (AXInPlaceObject.GetWindow(&hwndInPlaceObject).Succeeded()) { - Application.ParkHandle(new HandleRef(AXInPlaceObject, hwndInPlaceObject)); + Application.ParkHandle(new HandleRef(AXInPlaceObject, hwndInPlaceObject), DpiAwarenessContext); } } From dbe2f06b90291f922144175d00ab23844e6679d8 Mon Sep 17 00:00:00 2001 From: Devendar Reddy Adulla Date: Wed, 19 May 2021 16:13:54 -0700 Subject: [PATCH 2/8] Adding tests around Parking window creation in various contexts. --- .../Forms/Application.ThreadContext.cs | 6 +- .../src/System/Windows/Forms/Application.cs | 2 +- .../src/System/Windows/Forms/NativeWindow.cs | 6 +- .../UnitTests/HDPI/ParkingWindowTests.cs | 105 ++++++++++++++++++ 4 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Application.ThreadContext.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Application.ThreadContext.cs index 8ead9c08653..f6ad3ad7137 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Application.ThreadContext.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Application.ThreadContext.cs @@ -309,10 +309,10 @@ internal ParkingWindow GetParkingWindowForContext(IntPtr context) // Legacy OS/target framework scenario where ControlDpiContext is set to DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_UNSPECIFIED // because of 'ThreadContextDpiAwareness' API unavailability or this feature is not enabled. - if (_parkingWindows.Count == 1 - && (!DpiHelper.IsScalingRequirementMet - || User32.AreDpiAwarenessContextsEqual(context, User32.UNSPECIFIED_DPI_AWARENESS_CONTEXT))) + if (User32.AreDpiAwarenessContextsEqual(context, User32.UNSPECIFIED_DPI_AWARENESS_CONTEXT)) { + Debug.Assert(_parkingWindows.Count == 1, "parkingWindows count can not be > 1 for legacy OS/target framework versions"); + return _parkingWindows[0]; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs index 46ea1737e0f..267b3fecb57 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs @@ -1000,7 +1000,7 @@ private static Type GetAppMainType() /// /// Locates a thread context given a window handle. /// - private static ThreadContext GetContextForHandle(HandleRef handle) + internal static ThreadContext GetContextForHandle(HandleRef handle) { ThreadContext cxt = ThreadContext.FromId(User32.GetWindowThreadProcessId(handle.Handle, out _)); Debug.Assert( diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/NativeWindow.cs b/src/System.Windows.Forms/src/System/Windows/Forms/NativeWindow.cs index 2d0c4cdaa42..6331717de35 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/NativeWindow.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/NativeWindow.cs @@ -73,11 +73,9 @@ public NativeWindow() } /// - /// Cache window DpiContext awareness information that helps to create handle with right context at the later time. + /// Cache window DpiContext awareness information that helps to create handle with right context at the later time. /// - internal IntPtr DpiAwarenessContext { get; } = DpiHelper.IsScalingRequirementMet - ? User32.GetThreadDpiAwarenessContext() - : User32.UNSPECIFIED_DPI_AWARENESS_CONTEXT; + internal IntPtr DpiAwarenessContext { get; } = User32.GetThreadDpiAwarenessContext(); /// /// Override's the base object's finalize method. diff --git a/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs b/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs new file mode 100644 index 00000000000..39047ec1a63 --- /dev/null +++ b/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs @@ -0,0 +1,105 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Xunit; +using static System.Windows.Forms.Application; +using static Interop; + +namespace System.Windows.Forms.Tests.HDPI +{ + public partial class ParkingWindowTests : IClassFixture + { + [WinFormsFact] + public void UnawareParkingWindow() + { + //set process awareness + User32.SetProcessDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.UNAWARE)) + { + var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.UNAWARE); + Assert.NotNull(parkingWindow); + + var dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.UNAWARE, dpiContext)); + } + } + + [WinFormsFact] + public void SystemAwareParkingWindow() + { + //set process awareness + User32.SetProcessDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) + { + var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); + Assert.NotNull(parkingWindow); + + var dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); + } + } + + [WinFormsFact] + public void PMv2ParkingWindow() + { + //set process awareness + User32.SetProcessDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + var control = new Control(); + + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + + ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + Assert.NotNull(parkingWindow); + + var dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); + + } + + [WinFormsFact] + public void MultipleParkingWindows() + { + //set process awareness + User32.SetProcessDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + + var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + Assert.NotNull(parkingWindow); + + var dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); + + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) + { + var systemControl = new Control(); + cxt = GetContextForHandle(new HandleRef(this, systemControl.Handle)); + Assert.NotNull(cxt); + parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); + Assert.NotNull(parkingWindow); + + dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); + + //Checking PMv2 parking window still available. + parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + Assert.NotNull(parkingWindow); + + dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); + } + } + } +} From 67fb2f466c6573832c9ada4b2ea6772c1de66003 Mon Sep 17 00:00:00 2001 From: Devendar Reddy Adulla Date: Wed, 19 May 2021 16:25:47 -0700 Subject: [PATCH 3/8] Fixing build failure by analyzer. --- .../tests/UnitTests/HDPI/ParkingWindowTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs b/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs index 39047ec1a63..e7062a73cfd 100644 --- a/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs @@ -64,7 +64,6 @@ public void PMv2ParkingWindow() var dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); - } [WinFormsFact] From d4e57787ea9904949e9e5625aef17c16145dabab Mon Sep 17 00:00:00 2001 From: Devendar Reddy Adulla Date: Wed, 19 May 2021 17:29:29 -0700 Subject: [PATCH 4/8] Review feedback. --- .../UnitTests/HDPI/ParkingWindowTests.cs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs b/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs index e7062a73cfd..87fcab658f5 100644 --- a/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs @@ -14,18 +14,18 @@ public partial class ParkingWindowTests : IClassFixture [WinFormsFact] public void UnawareParkingWindow() { - //set process awareness + // set process awareness User32.SetProcessDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.UNAWARE)) { - var control = new Control(); + using var control = new Control(); ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); Assert.NotNull(cxt); - ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.UNAWARE); + using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.UNAWARE); Assert.NotNull(parkingWindow); - var dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.UNAWARE, dpiContext)); } } @@ -33,18 +33,18 @@ public void UnawareParkingWindow() [WinFormsFact] public void SystemAwareParkingWindow() { - //set process awareness + // set process awareness User32.SetProcessDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) { - var control = new Control(); + using var control = new Control(); ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); Assert.NotNull(cxt); - ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); + using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); Assert.NotNull(parkingWindow); - var dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); } } @@ -52,17 +52,17 @@ public void SystemAwareParkingWindow() [WinFormsFact] public void PMv2ParkingWindow() { - //set process awareness + // set process awareness User32.SetProcessDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - var control = new Control(); + using var control = new Control(); ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); Assert.NotNull(cxt); - ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); Assert.NotNull(parkingWindow); - var dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); } @@ -72,18 +72,18 @@ public void MultipleParkingWindows() //set process awareness User32.SetProcessDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - var control = new Control(); + using var control = new Control(); ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); Assert.NotNull(cxt); ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); Assert.NotNull(parkingWindow); - var dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) { - var systemControl = new Control(); + using var systemControl = new Control(); cxt = GetContextForHandle(new HandleRef(this, systemControl.Handle)); Assert.NotNull(cxt); parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); From 7cc8993133ee2977dbabfc873f17ff88711dff96 Mon Sep 17 00:00:00 2001 From: Devendar Reddy Adulla Date: Thu, 20 May 2021 11:15:32 -0700 Subject: [PATCH 5/8] Fixing test failures. Given that these failures are reproduced only in CI build and that too after adding new tests that were setting processwide dpi awareness, I am guessing filaure is because of incorrect DPIawareness between the threads while running tests. Replacing process wide DPI awareness with thread. --- .../UnitTests/HDPI/ParkingWindowTests.cs | 104 ---------------- .../Forms/Application.ParkingWindowTests.cs | 116 ++++++++++++++++++ 2 files changed, 116 insertions(+), 104 deletions(-) delete mode 100644 src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs create mode 100644 src/System.Windows.Forms/tests/UnitTests/HDPI/System/Windows/Forms/Application.ParkingWindowTests.cs diff --git a/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs b/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs deleted file mode 100644 index 87fcab658f5..00000000000 --- a/src/System.Windows.Forms/tests/UnitTests/HDPI/ParkingWindowTests.cs +++ /dev/null @@ -1,104 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Runtime.InteropServices; -using Xunit; -using static System.Windows.Forms.Application; -using static Interop; - -namespace System.Windows.Forms.Tests.HDPI -{ - public partial class ParkingWindowTests : IClassFixture - { - [WinFormsFact] - public void UnawareParkingWindow() - { - // set process awareness - User32.SetProcessDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.UNAWARE)) - { - using var control = new Control(); - ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); - Assert.NotNull(cxt); - using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.UNAWARE); - Assert.NotNull(parkingWindow); - - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.UNAWARE, dpiContext)); - } - } - - [WinFormsFact] - public void SystemAwareParkingWindow() - { - // set process awareness - User32.SetProcessDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) - { - using var control = new Control(); - ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); - Assert.NotNull(cxt); - using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); - Assert.NotNull(parkingWindow); - - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); - } - } - - [WinFormsFact] - public void PMv2ParkingWindow() - { - // set process awareness - User32.SetProcessDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - using var control = new Control(); - - ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); - Assert.NotNull(cxt); - - using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - Assert.NotNull(parkingWindow); - - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); - } - - [WinFormsFact] - public void MultipleParkingWindows() - { - //set process awareness - User32.SetProcessDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - - using var control = new Control(); - ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); - Assert.NotNull(cxt); - ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - Assert.NotNull(parkingWindow); - - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); - - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) - { - using var systemControl = new Control(); - cxt = GetContextForHandle(new HandleRef(this, systemControl.Handle)); - Assert.NotNull(cxt); - parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); - Assert.NotNull(parkingWindow); - - dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); - - //Checking PMv2 parking window still available. - parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - Assert.NotNull(parkingWindow); - - dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); - } - } - } -} diff --git a/src/System.Windows.Forms/tests/UnitTests/HDPI/System/Windows/Forms/Application.ParkingWindowTests.cs b/src/System.Windows.Forms/tests/UnitTests/HDPI/System/Windows/Forms/Application.ParkingWindowTests.cs new file mode 100644 index 00000000000..61ffb448e7c --- /dev/null +++ b/src/System.Windows.Forms/tests/UnitTests/HDPI/System/Windows/Forms/Application.ParkingWindowTests.cs @@ -0,0 +1,116 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Xunit; +using static System.Windows.Forms.Application; +using static Interop; + +namespace System.Windows.Forms.Tests.HDPI +{ + public partial class ParkingWindowTests : IClassFixture + { + [WinFormsFact] + public void UnawareParkingWindow() + { + // set thread awareness context to PMv2 + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2)) + { + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.UNAWARE)) + { + using var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.UNAWARE); + Assert.NotNull(parkingWindow); + + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.UNAWARE, dpiContext)); + } + } + } + + [WinFormsFact] + public void SystemAwareParkingWindow() + { + // set thread awareness context to PMv2 + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2)) + { + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) + { + using var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); + Assert.NotNull(parkingWindow); + + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); + } + } + } + + [WinFormsFact] + public void PMv2ParkingWindow() + { + // set thread awareness context to PMv2 + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2)) + { + using var control = new Control(); + + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + + using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + Assert.NotNull(parkingWindow); + + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); + } + } + + [WinFormsFact] + public void MultipleParkingWindows() + { + // set thread awareness context to PMv2 + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2)) + { + using var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + Assert.NotNull(parkingWindow); + + try + { + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); + + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) + { + using var systemControl = new Control(); + cxt = GetContextForHandle(new HandleRef(this, systemControl.Handle)); + Assert.NotNull(cxt); + parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); + Assert.NotNull(parkingWindow); + + dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); + + // check PMv2 parking window still available. + parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + Assert.NotNull(parkingWindow); + + dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); + } + } + finally + { + parkingWindow?.Dispose(); + } + } + } + } +} From f95f4da3be2a8aeee9271542500c4c155a1fdedf Mon Sep 17 00:00:00 2001 From: Devendar Reddy Adulla Date: Thu, 20 May 2021 11:30:53 -0700 Subject: [PATCH 6/8] reformatting tests. --- .../Forms/Application.ParkingWindowTests.cs | 130 ++++++++++-------- 1 file changed, 69 insertions(+), 61 deletions(-) diff --git a/src/System.Windows.Forms/tests/UnitTests/HDPI/System/Windows/Forms/Application.ParkingWindowTests.cs b/src/System.Windows.Forms/tests/UnitTests/HDPI/System/Windows/Forms/Application.ParkingWindowTests.cs index 61ffb448e7c..69c7a6ea861 100644 --- a/src/System.Windows.Forms/tests/UnitTests/HDPI/System/Windows/Forms/Application.ParkingWindowTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/HDPI/System/Windows/Forms/Application.ParkingWindowTests.cs @@ -15,102 +15,110 @@ public partial class ParkingWindowTests : IClassFixture public void UnawareParkingWindow() { // set thread awareness context to PMv2 - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2)) + IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.UNAWARE)) { - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.UNAWARE)) - { - using var control = new Control(); - ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); - Assert.NotNull(cxt); - using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.UNAWARE); - Assert.NotNull(parkingWindow); + using var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.UNAWARE); + Assert.NotNull(parkingWindow); - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.UNAWARE, dpiContext)); - } + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.UNAWARE, dpiContext)); } + + // reset back to original awareness context. + User32.SetThreadDpiAwarenessContext(originalAwarenessContext); } [WinFormsFact] public void SystemAwareParkingWindow() { // set thread awareness context to PMv2 - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2)) + IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) { - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) - { - using var control = new Control(); - ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); - Assert.NotNull(cxt); - using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); - Assert.NotNull(parkingWindow); + using var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); + Assert.NotNull(parkingWindow); - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); - } + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); } + + // reset back to original awareness context. + User32.SetThreadDpiAwarenessContext(originalAwarenessContext); } [WinFormsFact] public void PMv2ParkingWindow() { // set thread awareness context to PMv2 - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2)) - { - using var control = new Control(); + IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); - Assert.NotNull(cxt); + using var control = new Control(); - using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - Assert.NotNull(parkingWindow); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); - } + using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + Assert.NotNull(parkingWindow); + + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); + + // reset back to original awareness context. + User32.SetThreadDpiAwarenessContext(originalAwarenessContext); } [WinFormsFact] public void MultipleParkingWindows() { // set thread awareness context to PMv2 - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2)) + IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + + using var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + Assert.NotNull(parkingWindow); + + try { - using var control = new Control(); - ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); - Assert.NotNull(cxt); - ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - Assert.NotNull(parkingWindow); + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); - try + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) { - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); - - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) - { - using var systemControl = new Control(); - cxt = GetContextForHandle(new HandleRef(this, systemControl.Handle)); - Assert.NotNull(cxt); - parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); - Assert.NotNull(parkingWindow); + using var systemControl = new Control(); + cxt = GetContextForHandle(new HandleRef(this, systemControl.Handle)); + Assert.NotNull(cxt); + parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); + Assert.NotNull(parkingWindow); - dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); + dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); - // check PMv2 parking window still available. - parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - Assert.NotNull(parkingWindow); + // check PMv2 parking window still available. + parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + Assert.NotNull(parkingWindow); - dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); - } - } - finally - { - parkingWindow?.Dispose(); + dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); } } + finally + { + parkingWindow?.Dispose(); + } + + // reset back to original awareness context. + User32.SetThreadDpiAwarenessContext(originalAwarenessContext); } } } From fa48427862a659f54c0d3e18fd65e93f4d62b290 Mon Sep 17 00:00:00 2001 From: dreddy-work Date: Fri, 21 May 2021 00:31:02 -0700 Subject: [PATCH 7/8] Review suggestions. --- .../Forms/Application.ParkingWindowTests.cs | 124 ---------------- .../Forms/Application.ParkingWindowTests.cs | 132 ++++++++++++++++++ 2 files changed, 132 insertions(+), 124 deletions(-) delete mode 100644 src/System.Windows.Forms/tests/UnitTests/HDPI/System/Windows/Forms/Application.ParkingWindowTests.cs diff --git a/src/System.Windows.Forms/tests/UnitTests/HDPI/System/Windows/Forms/Application.ParkingWindowTests.cs b/src/System.Windows.Forms/tests/UnitTests/HDPI/System/Windows/Forms/Application.ParkingWindowTests.cs deleted file mode 100644 index 69c7a6ea861..00000000000 --- a/src/System.Windows.Forms/tests/UnitTests/HDPI/System/Windows/Forms/Application.ParkingWindowTests.cs +++ /dev/null @@ -1,124 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Runtime.InteropServices; -using Xunit; -using static System.Windows.Forms.Application; -using static Interop; - -namespace System.Windows.Forms.Tests.HDPI -{ - public partial class ParkingWindowTests : IClassFixture - { - [WinFormsFact] - public void UnawareParkingWindow() - { - // set thread awareness context to PMv2 - IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.UNAWARE)) - { - using var control = new Control(); - ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); - Assert.NotNull(cxt); - using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.UNAWARE); - Assert.NotNull(parkingWindow); - - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.UNAWARE, dpiContext)); - } - - // reset back to original awareness context. - User32.SetThreadDpiAwarenessContext(originalAwarenessContext); - } - - [WinFormsFact] - public void SystemAwareParkingWindow() - { - // set thread awareness context to PMv2 - IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) - { - using var control = new Control(); - ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); - Assert.NotNull(cxt); - using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); - Assert.NotNull(parkingWindow); - - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); - } - - // reset back to original awareness context. - User32.SetThreadDpiAwarenessContext(originalAwarenessContext); - } - - [WinFormsFact] - public void PMv2ParkingWindow() - { - // set thread awareness context to PMv2 - IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - - using var control = new Control(); - - ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); - Assert.NotNull(cxt); - - using ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - Assert.NotNull(parkingWindow); - - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); - - // reset back to original awareness context. - User32.SetThreadDpiAwarenessContext(originalAwarenessContext); - } - - [WinFormsFact] - public void MultipleParkingWindows() - { - // set thread awareness context to PMv2 - IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - - using var control = new Control(); - ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); - Assert.NotNull(cxt); - ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - Assert.NotNull(parkingWindow); - - try - { - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); - - using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) - { - using var systemControl = new Control(); - cxt = GetContextForHandle(new HandleRef(this, systemControl.Handle)); - Assert.NotNull(cxt); - parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); - Assert.NotNull(parkingWindow); - - dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); - - // check PMv2 parking window still available. - parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); - Assert.NotNull(parkingWindow); - - dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); - Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); - } - } - finally - { - parkingWindow?.Dispose(); - } - - // reset back to original awareness context. - User32.SetThreadDpiAwarenessContext(originalAwarenessContext); - } - } -} diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Application.ParkingWindowTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Application.ParkingWindowTests.cs index 270f232337b..adce4f6b173 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Application.ParkingWindowTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Application.ParkingWindowTests.cs @@ -57,5 +57,137 @@ private Form InitFormWithControlToGarbageCollect() return form; } + + + [WinFormsFact] + public void ParkingWindow_Unaware() + { + // set thread awareness context to PermonitorV2(PMv2). + // if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design. + // In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE + IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + + try + { + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.UNAWARE)) + { + using var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.UNAWARE); + Assert.NotNull(parkingWindow); + + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.UNAWARE, dpiContext)); + } + } + finally + { + // reset back to original awareness context. + User32.SetThreadDpiAwarenessContext(originalAwarenessContext); + } + } + + [WinFormsFact] + public void ParkingWindow_SystemAware() + { + // set thread awareness context to PermonitorV2(PMv2). + // if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design. + // In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE + IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + + try + { + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) + { + using var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); + Assert.NotNull(parkingWindow); + + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); + } + } + finally + { + // reset back to original awareness context. + User32.SetThreadDpiAwarenessContext(originalAwarenessContext); + } + } + + [WinFormsFact] + public void ParkingWindow_PermonitorV2() + { + // set thread awareness context to PermonitorV2(PMv2). + // if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design. + // In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE + IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + + try + { + using var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + + ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + Assert.NotNull(parkingWindow); + + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); + } + finally + { + // reset back to original awareness context. + User32.SetThreadDpiAwarenessContext(originalAwarenessContext); + } + } + + [WinFormsFact] + public void ParkingWindow_Multiple() + { + // set thread awareness context to PermonitorV2(PMv2). + // if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design. + // In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE + IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + + try + { + using var control = new Control(); + ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle)); + Assert.NotNull(cxt); + ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + Assert.NotNull(parkingWindow); + + + IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); + + using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) + { + using var systemControl = new Control(); + cxt = GetContextForHandle(new HandleRef(this, systemControl.Handle)); + Assert.NotNull(cxt); + parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); + Assert.NotNull(parkingWindow); + + dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); + + // check PMv2 parking window still available. + parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); + Assert.NotNull(parkingWindow); + + dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); + Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext)); + } + } + finally + { + // reset back to original awareness context. + User32.SetThreadDpiAwarenessContext(originalAwarenessContext); + } + } } } From ea3802e396709d73aed3ca7922313e8204b7b049 Mon Sep 17 00:00:00 2001 From: Devendar Reddy Adulla Date: Fri, 21 May 2021 01:08:40 -0700 Subject: [PATCH 8/8] Running tests only on Windows 10 --- .../Forms/Application.ParkingWindowTests.cs | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Application.ParkingWindowTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Application.ParkingWindowTests.cs index adce4f6b173..c7a439f55dc 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Application.ParkingWindowTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Application.ParkingWindowTests.cs @@ -2,10 +2,11 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Drawing; +using System.Runtime.InteropServices; using Microsoft.DotNet.RemoteExecutor; -using WinForms.Common.Tests; using Xunit; +using static System.Windows.Forms.Application; +using static Interop; namespace System.Windows.Forms.Tests { @@ -58,10 +59,15 @@ private Form InitFormWithControlToGarbageCollect() return form; } - [WinFormsFact] public void ParkingWindow_Unaware() { + // run tests only on Windows 10 versions that support thread dpi awareness. + if (!PlatformDetection.IsWindows10Version1803OrGreater) + { + return; + } + // set thread awareness context to PermonitorV2(PMv2). // if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design. // In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE @@ -91,6 +97,12 @@ public void ParkingWindow_Unaware() [WinFormsFact] public void ParkingWindow_SystemAware() { + // run tests only on Windows 10 versions that support thread dpi awareness. + if (!PlatformDetection.IsWindows10Version1803OrGreater) + { + return; + } + // set thread awareness context to PermonitorV2(PMv2). // if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design. // In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE @@ -120,6 +132,12 @@ public void ParkingWindow_SystemAware() [WinFormsFact] public void ParkingWindow_PermonitorV2() { + // run tests only on Windows 10 versions that support thread dpi awareness. + if (!PlatformDetection.IsWindows10Version1803OrGreater) + { + return; + } + // set thread awareness context to PermonitorV2(PMv2). // if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design. // In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE @@ -147,6 +165,12 @@ public void ParkingWindow_PermonitorV2() [WinFormsFact] public void ParkingWindow_Multiple() { + // run tests only on Windows 10 versions that support thread dpi awareness. + if (!PlatformDetection.IsWindows10Version1803OrGreater) + { + return; + } + // set thread awareness context to PermonitorV2(PMv2). // if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design. // In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE @@ -160,7 +184,6 @@ public void ParkingWindow_Multiple() ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); Assert.NotNull(parkingWindow); - IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext));