Skip to content

Commit 567c3b0

Browse files
committed
[dotnet] Add nullable reference annotations to Platform
1 parent ff7fa52 commit 567c3b0

File tree

3 files changed

+39
-93
lines changed

3 files changed

+39
-93
lines changed

dotnet/src/webdriver/Chromium/ChromiumDriverService.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ protected static string ChromiumDriverServiceFileName(string fileName = DefaultC
208208
// straightforward as you might hope.
209209
// See: http://mono.wikia.com/wiki/Detecting_the_execution_platform
210210
// and https://msdn.microsoft.com/en-us/library/3a8hyw88(v=vs.110).aspx
211-
const int PlatformMonoUnixValue = 128;
211+
const PlatformID PlatformIDMonoUnix = (PlatformID)128;
212212

213213
switch (Environment.OSVersion.Platform)
214214
{
@@ -221,17 +221,14 @@ protected static string ChromiumDriverServiceFileName(string fileName = DefaultC
221221

222222
case PlatformID.MacOSX:
223223
case PlatformID.Unix:
224+
case PlatformIDMonoUnix:
224225
break;
225226

226227
// Don't handle the Xbox case. Let default handle it.
227228
// case PlatformID.Xbox:
228229
// break;
229-
default:
230-
if ((int)Environment.OSVersion.Platform == PlatformMonoUnixValue)
231-
{
232-
break;
233-
}
234230

231+
default:
235232
throw new WebDriverException("Unsupported platform: " + Environment.OSVersion.Platform);
236233
}
237234

dotnet/src/webdriver/Firefox/FirefoxDriverService.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ private static string FirefoxDriverServiceFileName()
263263
// straightforward as you might hope.
264264
// See: http://mono.wikia.com/wiki/Detecting_the_execution_platform
265265
// and https://msdn.microsoft.com/en-us/library/3a8hyw88(v=vs.110).aspx
266-
const int PlatformMonoUnixValue = 128;
266+
const PlatformID PlatformIDMonoUnix = (PlatformID)128;
267267

268268
switch (Environment.OSVersion.Platform)
269269
{
@@ -276,17 +276,13 @@ private static string FirefoxDriverServiceFileName()
276276

277277
case PlatformID.MacOSX:
278278
case PlatformID.Unix:
279+
case PlatformIDMonoUnix:
279280
break;
280281

281282
// Don't handle the Xbox case. Let default handle it.
282283
// case PlatformID.Xbox:
283284
// break;
284285
default:
285-
if ((int)Environment.OSVersion.Platform == PlatformMonoUnixValue)
286-
{
287-
break;
288-
}
289-
290286
throw new WebDriverException("Unsupported platform: " + Environment.OSVersion.Platform);
291287
}
292288

dotnet/src/webdriver/Platform.cs

Lines changed: 34 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
using System;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium
2325
{
2426
/// <summary>
@@ -84,138 +86,92 @@ public enum PlatformType
8486
/// </summary>
8587
public class Platform
8688
{
87-
private static Platform current;
88-
private PlatformType platformTypeValue;
89-
private int major;
90-
private int minor;
89+
private static Platform? current;
9190

9291
/// <summary>
9392
/// Initializes a new instance of the <see cref="Platform"/> class for a specific platform type.
9493
/// </summary>
9594
/// <param name="typeValue">The platform type.</param>
9695
public Platform(PlatformType typeValue)
9796
{
98-
this.platformTypeValue = typeValue;
97+
this.PlatformType = typeValue;
9998
}
10099

101100
private Platform()
102101
{
103-
this.major = Environment.OSVersion.Version.Major;
104-
this.minor = Environment.OSVersion.Version.Minor;
102+
this.MajorVersion = Environment.OSVersion.Version.Major;
103+
this.MinorVersion = Environment.OSVersion.Version.Minor;
105104

106105
switch (Environment.OSVersion.Platform)
107106
{
108107
case PlatformID.Win32NT:
109-
if (this.major == 5)
108+
if (this.MajorVersion == 5)
110109
{
111-
this.platformTypeValue = PlatformType.XP;
110+
this.PlatformType = PlatformType.XP;
112111
}
113-
else if (this.major == 6)
112+
else if (this.MajorVersion == 6)
114113
{
115-
this.platformTypeValue = PlatformType.Vista;
114+
this.PlatformType = PlatformType.Vista;
116115
}
117116
else
118117
{
119-
this.platformTypeValue = PlatformType.Windows;
118+
this.PlatformType = PlatformType.Windows;
120119
}
121120

122121
break;
123122

124123
// 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
125124
case PlatformID.MacOSX:
126-
this.platformTypeValue = PlatformType.Mac;
125+
this.PlatformType = PlatformType.Mac;
127126
break;
128127

129128
case PlatformID.Unix:
130-
this.platformTypeValue = PlatformType.Unix;
129+
this.PlatformType = PlatformType.Unix;
131130
break;
132131
}
133132
}
134133

135134
/// <summary>
136135
/// Gets the current platform.
137136
/// </summary>
138-
public static Platform CurrentPlatform
139-
{
140-
get
141-
{
142-
if (current == null)
143-
{
144-
current = new Platform();
145-
}
146-
147-
return current;
148-
}
149-
}
137+
public static Platform CurrentPlatform => current ??= new Platform();
150138

151139
/// <summary>
152140
/// Gets the major version of the platform operating system.
153141
/// </summary>
154-
public int MajorVersion
155-
{
156-
get { return this.major; }
157-
}
142+
public int MajorVersion { get; }
158143

159144
/// <summary>
160145
/// Gets the major version of the platform operating system.
161146
/// </summary>
162-
public int MinorVersion
163-
{
164-
get { return this.minor; }
165-
}
147+
public int MinorVersion { get; }
166148

167149
/// <summary>
168150
/// Gets the type of the platform.
169151
/// </summary>
170-
public PlatformType PlatformType
171-
{
172-
get { return this.platformTypeValue; }
173-
}
152+
public PlatformType PlatformType { get; }
174153

175154
/// <summary>
176155
/// Gets the value of the platform type for transmission using the JSON Wire Protocol.
177156
/// </summary>
178-
public string ProtocolPlatformType
179-
{
180-
get { return this.platformTypeValue.ToString("G").ToUpperInvariant(); }
181-
}
157+
public string ProtocolPlatformType => this.PlatformType.ToString("G").ToUpperInvariant();
182158

183159
/// <summary>
184160
/// Compares the platform to the specified type.
185161
/// </summary>
186-
/// <param name="compareTo">A <see cref="PlatformType"/> value to compare to.</param>
162+
/// <param name="compareTo">A <see cref="Selenium.PlatformType"/> value to compare to.</param>
187163
/// <returns><see langword="true"/> if the platforms match; otherwise <see langword="false"/>.</returns>
188164
public bool IsPlatformType(PlatformType compareTo)
189165
{
190-
bool platformIsType = false;
191-
switch (compareTo)
166+
return compareTo switch
192167
{
193-
case PlatformType.Any:
194-
platformIsType = true;
195-
break;
196-
197-
case PlatformType.Windows:
198-
platformIsType = this.platformTypeValue == PlatformType.Windows || this.platformTypeValue == PlatformType.XP || this.platformTypeValue == PlatformType.Vista;
199-
break;
200-
201-
case PlatformType.Vista:
202-
platformIsType = this.platformTypeValue == PlatformType.Windows || this.platformTypeValue == PlatformType.Vista;
203-
break;
204-
205-
case PlatformType.XP:
206-
platformIsType = this.platformTypeValue == PlatformType.Windows || this.platformTypeValue == PlatformType.XP;
207-
break;
208-
209-
case PlatformType.Linux:
210-
platformIsType = this.platformTypeValue == PlatformType.Linux || this.platformTypeValue == PlatformType.Unix;
211-
break;
212-
213-
default:
214-
platformIsType = this.platformTypeValue == compareTo;
215-
break;
216-
}
217-
218-
return platformIsType;
168+
PlatformType.Any => true,
169+
PlatformType.Windows => this.PlatformType is PlatformType.Windows or PlatformType.XP or PlatformType.Vista,
170+
PlatformType.Vista => this.PlatformType is PlatformType.Windows or PlatformType.Vista,
171+
PlatformType.XP => this.PlatformType is PlatformType.Windows or PlatformType.XP,
172+
PlatformType.Linux => this.PlatformType is PlatformType.Linux or PlatformType.Unix,
173+
_ => this.PlatformType == compareTo,
174+
};
219175
}
220176

221177
/// <summary>
@@ -224,7 +180,7 @@ public bool IsPlatformType(PlatformType compareTo)
224180
/// <returns>The string value for this platform type.</returns>
225181
public override string ToString()
226182
{
227-
return this.platformTypeValue.ToString();
183+
return this.PlatformType.ToString();
228184
}
229185

230186
/// <summary>
@@ -234,18 +190,15 @@ public override string ToString()
234190
/// <returns>The Platform object represented by the string name.</returns>
235191
internal static Platform FromString(string platformName)
236192
{
237-
PlatformType platformTypeFromString = PlatformType.Any;
238-
try
193+
if (Enum.TryParse(platformName, ignoreCase: true, out PlatformType platformTypeFromString))
239194
{
240-
platformTypeFromString = (PlatformType)Enum.Parse(typeof(PlatformType), platformName, true);
241-
}
242-
catch (ArgumentException)
243-
{
244-
// If the requested platform string is not a valid platform type,
245-
// ignore it and use PlatformType.Any.
195+
return new Platform(platformTypeFromString);
246196
}
247197

248-
return new Platform(platformTypeFromString);
198+
// If the requested platform string is not a valid platform type,
199+
// ignore it and use PlatformType.Any.
200+
201+
return new Platform(PlatformType.Any);
249202
}
250203
}
251204
}

0 commit comments

Comments
 (0)