From cacc597a8411ddf815c4676acc6c95477c1a49b4 Mon Sep 17 00:00:00 2001 From: Igor Velikorossov Date: Fri, 14 May 2021 21:18:40 +1000 Subject: [PATCH] Validate input in Application.SetHighDpiMode() method --- .../src/System/Windows/Forms/Application.cs | 20 +++++++++++-------- .../System/Windows/Forms/ApplicationTests.cs | 10 +++++++++- 2 files changed, 21 insertions(+), 9 deletions(-) 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..b855665ab5f 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs @@ -303,14 +303,6 @@ public static string ExecutablePath public static HighDpiMode HighDpiMode => DpiHelper.GetWinformsApplicationDpiAwareness(); - /// - /// Sets the mode for process. - /// - /// One of the enumeration values that specifies the high DPI mode to set. - /// if the high DPI mode was set; otherwise, . - public static bool SetHighDpiMode(HighDpiMode highDpiMode) - => !DpiHelper.FirstParkingWindowCreated && DpiHelper.SetWinformsApplicationDpiAwareness(highDpiMode); - /// /// Gets the path for the application data specific to a local, non-roaming user. /// @@ -1241,6 +1233,18 @@ public static void SetCompatibleTextRenderingDefault(bool defaultValue) Control.UseCompatibleTextRenderingDefault = defaultValue; } + /// + /// Sets the mode for process. + /// + /// One of the enumeration values that specifies the high DPI mode to set. + /// if the high DPI mode was set; otherwise, . + public static bool SetHighDpiMode(HighDpiMode highDpiMode) + { + SourceGenerated.EnumValidator.Validate(highDpiMode, nameof(highDpiMode)); + + return !DpiHelper.FirstParkingWindowCreated && DpiHelper.SetWinformsApplicationDpiAwareness(highDpiMode); + } + /// /// Sets the suspend/hibernate state of the machine. /// Returns true if the call succeeded, else false. diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ApplicationTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ApplicationTests.cs index d691ece2a46..1a03bd95d29 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ApplicationTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ApplicationTests.cs @@ -1,8 +1,9 @@ -// Licensed to the .NET Foundation under one or more agreements. +// 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.Collections.Generic; +using System.ComponentModel; using System.Globalization; using System.IO; using System.Threading; @@ -150,6 +151,13 @@ public void Application_EnableVisualStyles_ManifestResourceExists() Assert.NotNull(stream); } + [WinFormsTheory] + [CommonMemberData(nameof(CommonTestHelper.GetEnumTypeTheoryDataInvalid), typeof(HighDpiMode))] + public void Application_SetHighDpiMode_SetInvalidValue_ThrowsInvalidEnumArgumentException(HighDpiMode value) + { + Assert.Throws("highDpiMode", () => Application.SetHighDpiMode(value)); + } + private class CustomLCIDCultureInfo : CultureInfo { private readonly int _lcid;