diff --git a/dotnet/src/webdriver/Chromium/ChromiumDriverService.cs b/dotnet/src/webdriver/Chromium/ChromiumDriverService.cs index 04a4e8994118f..95958e44812f4 100644 --- a/dotnet/src/webdriver/Chromium/ChromiumDriverService.cs +++ b/dotnet/src/webdriver/Chromium/ChromiumDriverService.cs @@ -208,7 +208,7 @@ protected static string ChromiumDriverServiceFileName(string fileName = DefaultC // straightforward as you might hope. // See: http://mono.wikia.com/wiki/Detecting_the_execution_platform // and https://msdn.microsoft.com/en-us/library/3a8hyw88(v=vs.110).aspx - const int PlatformMonoUnixValue = 128; + const PlatformID PlatformIDMonoUnix = (PlatformID)128; switch (Environment.OSVersion.Platform) { @@ -221,17 +221,14 @@ protected static string ChromiumDriverServiceFileName(string fileName = DefaultC case PlatformID.MacOSX: case PlatformID.Unix: + case PlatformIDMonoUnix: break; // Don't handle the Xbox case. Let default handle it. // case PlatformID.Xbox: // break; - default: - if ((int)Environment.OSVersion.Platform == PlatformMonoUnixValue) - { - break; - } + default: throw new WebDriverException("Unsupported platform: " + Environment.OSVersion.Platform); } diff --git a/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs b/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs index 6c58457461a22..3d35057e69e64 100644 --- a/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs +++ b/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs @@ -263,7 +263,7 @@ private static string FirefoxDriverServiceFileName() // straightforward as you might hope. // See: http://mono.wikia.com/wiki/Detecting_the_execution_platform // and https://msdn.microsoft.com/en-us/library/3a8hyw88(v=vs.110).aspx - const int PlatformMonoUnixValue = 128; + const PlatformID PlatformIDMonoUnix = (PlatformID)128; switch (Environment.OSVersion.Platform) { @@ -276,17 +276,13 @@ private static string FirefoxDriverServiceFileName() case PlatformID.MacOSX: case PlatformID.Unix: + case PlatformIDMonoUnix: break; // Don't handle the Xbox case. Let default handle it. // case PlatformID.Xbox: // break; default: - if ((int)Environment.OSVersion.Platform == PlatformMonoUnixValue) - { - break; - } - throw new WebDriverException("Unsupported platform: " + Environment.OSVersion.Platform); } diff --git a/dotnet/src/webdriver/Platform.cs b/dotnet/src/webdriver/Platform.cs index c143fd682d3b7..4ea2cc9be61c8 100644 --- a/dotnet/src/webdriver/Platform.cs +++ b/dotnet/src/webdriver/Platform.cs @@ -19,6 +19,8 @@ using System; +#nullable enable + namespace OpenQA.Selenium { /// @@ -84,10 +86,7 @@ public enum PlatformType /// public class Platform { - private static Platform current; - private PlatformType platformTypeValue; - private int major; - private int minor; + private static Platform? current; /// /// Initializes a new instance of the class for a specific platform type. @@ -95,39 +94,39 @@ public class Platform /// The platform type. public Platform(PlatformType typeValue) { - this.platformTypeValue = typeValue; + this.PlatformType = typeValue; } private Platform() { - this.major = Environment.OSVersion.Version.Major; - this.minor = Environment.OSVersion.Version.Minor; + this.MajorVersion = Environment.OSVersion.Version.Major; + this.MinorVersion = Environment.OSVersion.Version.Minor; switch (Environment.OSVersion.Platform) { case PlatformID.Win32NT: - if (this.major == 5) + if (this.MajorVersion == 5) { - this.platformTypeValue = PlatformType.XP; + this.PlatformType = PlatformType.XP; } - else if (this.major == 6) + else if (this.MajorVersion == 6) { - this.platformTypeValue = PlatformType.Vista; + this.PlatformType = PlatformType.Vista; } else { - this.platformTypeValue = PlatformType.Windows; + this.PlatformType = PlatformType.Windows; } break; // Thanks to a bug in Mono Mac and Linux will be treated the same https://bugzilla.novell.com/show_bug.cgi?id=515570 but adding this in case case PlatformID.MacOSX: - this.platformTypeValue = PlatformType.Mac; + this.PlatformType = PlatformType.Mac; break; case PlatformID.Unix: - this.platformTypeValue = PlatformType.Unix; + this.PlatformType = PlatformType.Unix; break; } } @@ -135,87 +134,44 @@ private Platform() /// /// Gets the current platform. /// - public static Platform CurrentPlatform - { - get - { - if (current == null) - { - current = new Platform(); - } - - return current; - } - } + public static Platform CurrentPlatform => current ??= new Platform(); /// /// Gets the major version of the platform operating system. /// - public int MajorVersion - { - get { return this.major; } - } + public int MajorVersion { get; } /// /// Gets the major version of the platform operating system. /// - public int MinorVersion - { - get { return this.minor; } - } + public int MinorVersion { get; } /// /// Gets the type of the platform. /// - public PlatformType PlatformType - { - get { return this.platformTypeValue; } - } + public PlatformType PlatformType { get; } /// /// Gets the value of the platform type for transmission using the JSON Wire Protocol. /// - public string ProtocolPlatformType - { - get { return this.platformTypeValue.ToString("G").ToUpperInvariant(); } - } + public string ProtocolPlatformType => this.PlatformType.ToString("G").ToUpperInvariant(); /// /// Compares the platform to the specified type. /// - /// A value to compare to. + /// A value to compare to. /// if the platforms match; otherwise . public bool IsPlatformType(PlatformType compareTo) { - bool platformIsType = false; - switch (compareTo) + return compareTo switch { - case PlatformType.Any: - platformIsType = true; - break; - - case PlatformType.Windows: - platformIsType = this.platformTypeValue == PlatformType.Windows || this.platformTypeValue == PlatformType.XP || this.platformTypeValue == PlatformType.Vista; - break; - - case PlatformType.Vista: - platformIsType = this.platformTypeValue == PlatformType.Windows || this.platformTypeValue == PlatformType.Vista; - break; - - case PlatformType.XP: - platformIsType = this.platformTypeValue == PlatformType.Windows || this.platformTypeValue == PlatformType.XP; - break; - - case PlatformType.Linux: - platformIsType = this.platformTypeValue == PlatformType.Linux || this.platformTypeValue == PlatformType.Unix; - break; - - default: - platformIsType = this.platformTypeValue == compareTo; - break; - } - - return platformIsType; + PlatformType.Any => true, + PlatformType.Windows => this.PlatformType is PlatformType.Windows or PlatformType.XP or PlatformType.Vista, + PlatformType.Vista => this.PlatformType is PlatformType.Windows or PlatformType.Vista, + PlatformType.XP => this.PlatformType is PlatformType.Windows or PlatformType.XP, + PlatformType.Linux => this.PlatformType is PlatformType.Linux or PlatformType.Unix, + _ => this.PlatformType == compareTo, + }; } /// @@ -224,7 +180,7 @@ public bool IsPlatformType(PlatformType compareTo) /// The string value for this platform type. public override string ToString() { - return this.platformTypeValue.ToString(); + return this.PlatformType.ToString(); } /// @@ -234,18 +190,15 @@ public override string ToString() /// The Platform object represented by the string name. internal static Platform FromString(string platformName) { - PlatformType platformTypeFromString = PlatformType.Any; - try + if (Enum.TryParse(platformName, ignoreCase: true, out PlatformType platformTypeFromString)) { - platformTypeFromString = (PlatformType)Enum.Parse(typeof(PlatformType), platformName, true); - } - catch (ArgumentException) - { - // If the requested platform string is not a valid platform type, - // ignore it and use PlatformType.Any. + return new Platform(platformTypeFromString); } - return new Platform(platformTypeFromString); + // If the requested platform string is not a valid platform type, + // ignore it and use PlatformType.Any. + + return new Platform(PlatformType.Any); } } }