Skip to content

Commit

Permalink
[dotnet] Add nullable reference annotations to Platform (#14834)
Browse files Browse the repository at this point in the history
  • Loading branch information
RenderMichael authored Nov 30, 2024
1 parent ff7fa52 commit 209e275
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 93 deletions.
9 changes: 3 additions & 6 deletions dotnet/src/webdriver/Chromium/ChromiumDriverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}

Expand Down
8 changes: 2 additions & 6 deletions dotnet/src/webdriver/Firefox/FirefoxDriverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}

Expand Down
115 changes: 34 additions & 81 deletions dotnet/src/webdriver/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using System;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down Expand Up @@ -84,138 +86,92 @@ public enum PlatformType
/// </summary>
public class Platform
{
private static Platform current;
private PlatformType platformTypeValue;
private int major;
private int minor;
private static Platform? current;

/// <summary>
/// Initializes a new instance of the <see cref="Platform"/> class for a specific platform type.
/// </summary>
/// <param name="typeValue">The platform type.</param>
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;
}
}

/// <summary>
/// Gets the current platform.
/// </summary>
public static Platform CurrentPlatform
{
get
{
if (current == null)
{
current = new Platform();
}

return current;
}
}
public static Platform CurrentPlatform => current ??= new Platform();

/// <summary>
/// Gets the major version of the platform operating system.
/// </summary>
public int MajorVersion
{
get { return this.major; }
}
public int MajorVersion { get; }

/// <summary>
/// Gets the major version of the platform operating system.
/// </summary>
public int MinorVersion
{
get { return this.minor; }
}
public int MinorVersion { get; }

/// <summary>
/// Gets the type of the platform.
/// </summary>
public PlatformType PlatformType
{
get { return this.platformTypeValue; }
}
public PlatformType PlatformType { get; }

/// <summary>
/// Gets the value of the platform type for transmission using the JSON Wire Protocol.
/// </summary>
public string ProtocolPlatformType
{
get { return this.platformTypeValue.ToString("G").ToUpperInvariant(); }
}
public string ProtocolPlatformType => this.PlatformType.ToString("G").ToUpperInvariant();

/// <summary>
/// Compares the platform to the specified type.
/// </summary>
/// <param name="compareTo">A <see cref="PlatformType"/> value to compare to.</param>
/// <param name="compareTo">A <see cref="Selenium.PlatformType"/> value to compare to.</param>
/// <returns><see langword="true"/> if the platforms match; otherwise <see langword="false"/>.</returns>
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,
};
}

/// <summary>
Expand All @@ -224,7 +180,7 @@ public bool IsPlatformType(PlatformType compareTo)
/// <returns>The string value for this platform type.</returns>
public override string ToString()
{
return this.platformTypeValue.ToString();
return this.PlatformType.ToString();
}

/// <summary>
Expand All @@ -234,18 +190,15 @@ public override string ToString()
/// <returns>The Platform object represented by the string name.</returns>
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);
}
}
}

0 comments on commit 209e275

Please sign in to comment.