Skip to content

Commit 4666186

Browse files
[dotnet] Add nullability to Chromium configuration types (#15204)
1 parent 5bad332 commit 4666186

4 files changed

+45
-110
lines changed

dotnet/src/webdriver/Chromium/ChromiumAndroidOptions.cs

+4-13
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919

2020
using OpenQA.Selenium.Internal;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.Chromium
2325
{
2426
/// <summary>
2527
/// Generates the capabilities for automating Chromium applications on Android
2628
/// </summary>
2729
public class ChromiumAndroidOptions : AndroidOptions
2830
{
29-
private string androidProcess;
30-
private bool androidUseRunningApp;
31-
3231
/// <summary>
3332
/// Initializes a new instance of the <see cref="ChromiumAndroidOptions"/> class.
3433
/// </summary>
@@ -40,19 +39,11 @@ public ChromiumAndroidOptions(string androidPackage) : base(androidPackage)
4039
/// <summary>
4140
/// Gets or sets a value indicating whether to use an already running app.
4241
/// </summary>
43-
public bool UseRunningApp
44-
{
45-
get { return this.androidUseRunningApp; }
46-
set { this.androidUseRunningApp = value; }
47-
}
42+
public bool UseRunningApp { get; set; }
4843

4944
/// <summary>
5045
/// Gets or sets the process name of the Activity hosting the app.
5146
/// </summary>
52-
public string AndroidProcess
53-
{
54-
get { return this.androidProcess; }
55-
set { this.androidProcess = value; }
56-
}
47+
public string? AndroidProcess { get; set; }
5748
}
5849
}

dotnet/src/webdriver/Chromium/ChromiumMobileEmulationDeviceSettings.cs

+9-33
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// under the License.
1818
// </copyright>
1919

20+
#nullable enable
21+
2022
namespace OpenQA.Selenium.Chromium
2123
{
2224
/// <summary>
@@ -25,12 +27,6 @@ namespace OpenQA.Selenium.Chromium
2527
/// </summary>
2628
public class ChromiumMobileEmulationDeviceSettings
2729
{
28-
private string userAgent = string.Empty;
29-
private long width;
30-
private long height;
31-
private double pixelRatio;
32-
private bool enableTouchEvents = true;
33-
3430
/// <summary>
3531
/// Initializes a new instance of the <see cref="ChromiumMobileEmulationDeviceSettings"/> class.
3632
/// </summary>
@@ -43,59 +39,39 @@ public ChromiumMobileEmulationDeviceSettings()
4339
/// </summary>
4440
/// <param name="userAgent">The user agent string to be used by the browser when emulating
4541
/// a mobile device.</param>
46-
public ChromiumMobileEmulationDeviceSettings(string userAgent)
42+
public ChromiumMobileEmulationDeviceSettings(string? userAgent)
4743
{
48-
this.userAgent = userAgent;
44+
this.UserAgent = userAgent;
4945
}
5046

5147
/// <summary>
5248
/// Gets or sets the user agent string to be used by the browser when emulating
5349
/// a mobile device.
5450
/// </summary>
55-
public string UserAgent
56-
{
57-
get { return this.userAgent; }
58-
set { this.userAgent = value; }
59-
}
51+
public string? UserAgent { get; set; }
6052

6153
/// <summary>
6254
/// Gets or sets the width in pixels to be used by the browser when emulating
6355
/// a mobile device.
6456
/// </summary>
65-
public long Width
66-
{
67-
get { return this.width; }
68-
set { this.width = value; }
69-
}
57+
public long Width { get; set; }
7058

7159
/// <summary>
7260
/// Gets or sets the height in pixels to be used by the browser when emulating
7361
/// a mobile device.
7462
/// </summary>
75-
public long Height
76-
{
77-
get { return this.height; }
78-
set { this.height = value; }
79-
}
63+
public long Height { get; set; }
8064

8165
/// <summary>
8266
/// Gets or sets the pixel ratio to be used by the browser when emulating
8367
/// a mobile device.
8468
/// </summary>
85-
public double PixelRatio
86-
{
87-
get { return this.pixelRatio; }
88-
set { this.pixelRatio = value; }
89-
}
69+
public double PixelRatio { get; set; }
9070

9171
/// <summary>
9272
/// Gets or sets a value indicating whether touch events should be enabled by
9373
/// the browser when emulating a mobile device. Defaults to <see langword="true"/>.
9474
/// </summary>
95-
public bool EnableTouchEvents
96-
{
97-
get { return this.enableTouchEvents; }
98-
set { this.enableTouchEvents = value; }
99-
}
75+
public bool EnableTouchEvents { get; set; } = true;
10076
}
10177
}

dotnet/src/webdriver/Chromium/ChromiumNetworkConditions.cs

+18-31
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,42 @@
2121
using System.Collections.Generic;
2222
using System.Text.Json.Serialization;
2323

24+
#nullable enable
25+
2426
namespace OpenQA.Selenium.Chromium
2527
{
2628
/// <summary>
2729
/// Provides manipulation of getting and setting network conditions from Chromium.
2830
/// </summary>
2931
public class ChromiumNetworkConditions
3032
{
31-
private bool offline;
32-
private TimeSpan latency = TimeSpan.Zero;
3333
private long downloadThroughput = 0;
3434
private long uploadThroughput = 0;
3535

3636
/// <summary>
3737
/// Gets or sets a value indicating whether the network is offline. Defaults to <see langword="false"/>.
3838
/// </summary>
3939
[JsonPropertyName("offline")]
40-
public bool IsOffline
41-
{
42-
get { return this.offline; }
43-
set { this.offline = value; }
44-
}
40+
public bool IsOffline { get; set; }
4541

4642
/// <summary>
4743
/// Gets or sets the simulated latency of the connection. Typically given in milliseconds.
4844
/// </summary>
4945
[JsonIgnore]
50-
public TimeSpan Latency
51-
{
52-
get { return this.latency; }
53-
set { this.latency = value; }
54-
}
46+
public TimeSpan Latency { get; set; } = TimeSpan.Zero;
5547

5648
/// <summary>
5749
/// Gets or sets the throughput of the network connection in bytes/second for downloading.
5850
/// </summary>
5951
[JsonPropertyName("download_throughput")]
6052
public long DownloadThroughput
6153
{
62-
get { return this.downloadThroughput; }
54+
get => this.downloadThroughput;
6355
set
6456
{
6557
if (value < 0)
6658
{
67-
throw new WebDriverException("Downlod throughput cannot be negative.");
59+
throw new WebDriverException("Download throughput cannot be negative.");
6860
}
6961

7062
this.downloadThroughput = value;
@@ -77,7 +69,7 @@ public long DownloadThroughput
7769
[JsonPropertyName("upload_throughput")]
7870
public long UploadThroughput
7971
{
80-
get { return this.uploadThroughput; }
72+
get => this.uploadThroughput;
8173
set
8274
{
8375
if (value < 0)
@@ -92,13 +84,7 @@ public long UploadThroughput
9284
[JsonPropertyName("latency")]
9385
[JsonInclude]
9486
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
95-
internal long? SerializableLatency
96-
{
97-
get
98-
{
99-
return Convert.ToInt64(this.latency.TotalMilliseconds);
100-
}
101-
}
87+
internal long? SerializableLatency => Convert.ToInt64(this.Latency.TotalMilliseconds);
10288

10389
/// <summary>
10490
/// Creates a ChromiumNetworkConditions object from a dictionary of key-value pairs.
@@ -108,24 +94,24 @@ internal long? SerializableLatency
10894
public static ChromiumNetworkConditions FromDictionary(Dictionary<string, object> dictionary)
10995
{
11096
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
111-
if (dictionary.ContainsKey("offline"))
97+
if (dictionary.TryGetValue("offline", out object? offline))
11298
{
113-
conditions.IsOffline = (bool)dictionary["offline"];
99+
conditions.IsOffline = (bool)offline;
114100
}
115101

116-
if (dictionary.ContainsKey("latency"))
102+
if (dictionary.TryGetValue("latency", out object? latency))
117103
{
118-
conditions.Latency = TimeSpan.FromMilliseconds(Convert.ToDouble(dictionary["latency"]));
104+
conditions.Latency = TimeSpan.FromMilliseconds(Convert.ToDouble(latency));
119105
}
120106

121-
if (dictionary.ContainsKey("upload_throughput"))
107+
if (dictionary.TryGetValue("upload_throughput", out object? uploadThroughput))
122108
{
123-
conditions.UploadThroughput = (long)dictionary["upload_throughput"];
109+
conditions.UploadThroughput = (long)uploadThroughput;
124110
}
125111

126-
if (dictionary.ContainsKey("download_throughput"))
112+
if (dictionary.TryGetValue("download_throughput", out object? downloadThroughput))
127113
{
128-
conditions.DownloadThroughput = (long)dictionary["download_throughput"];
114+
conditions.DownloadThroughput = (long)downloadThroughput;
129115
}
130116

131117
return conditions;
@@ -135,11 +121,12 @@ public static ChromiumNetworkConditions FromDictionary(Dictionary<string, object
135121
/// Sets the upload and download throughput properties to the same value.
136122
/// </summary>
137123
/// <param name="throughput">The throughput of the network connection in bytes/second for both upload and download.</param>
124+
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="throughput"/> is negative.</exception>
138125
public void SetBidirectionalThroughput(long throughput)
139126
{
140127
if (throughput < 0)
141128
{
142-
throw new ArgumentException("Throughput values cannot be negative.", nameof(throughput));
129+
throw new ArgumentOutOfRangeException(nameof(throughput), "Throughput values cannot be negative.");
143130
}
144131

145132
this.uploadThroughput = throughput;

dotnet/src/webdriver/Chromium/ChromiumPerformanceLoggingPreferences.cs

+14-33
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using System;
2121
using System.Collections.Generic;
2222

23+
#nullable enable
24+
2325
namespace OpenQA.Selenium.Chromium
2426
{
2527
/// <summary>
@@ -28,49 +30,36 @@ namespace OpenQA.Selenium.Chromium
2830
/// </summary>
2931
public class ChromiumPerformanceLoggingPreferences
3032
{
31-
private bool isCollectingNetworkEvents = true;
32-
private bool isCollectingPageEvents = true;
3333
private TimeSpan bufferUsageReportingInterval = TimeSpan.FromMilliseconds(1000);
34-
private List<string> tracingCategories = new List<string>();
34+
private readonly List<string> tracingCategories = new List<string>();
3535

3636
/// <summary>
3737
/// Gets or sets a value indicating whether Chromium will collect events from the Network domain.
3838
/// Defaults to <see langword="true"/>.
3939
/// </summary>
40-
public bool IsCollectingNetworkEvents
41-
{
42-
get { return this.isCollectingNetworkEvents; }
43-
set { this.isCollectingNetworkEvents = value; }
44-
}
40+
public bool IsCollectingNetworkEvents { get; set; } = true;
4541

4642
/// <summary>
4743
/// Gets or sets a value indicating whether Chromium will collect events from the Page domain.
4844
/// Defaults to <see langword="true"/>.
4945
/// </summary>
50-
public bool IsCollectingPageEvents
51-
{
52-
get { return this.isCollectingPageEvents; }
53-
set { this.isCollectingPageEvents = value; }
54-
}
46+
public bool IsCollectingPageEvents { get; set; } = true;
5547

5648
/// <summary>
5749
/// Gets or sets the interval between Chromium DevTools trace buffer usage events.
5850
/// Defaults to 1000 milliseconds.
5951
/// </summary>
6052
/// <exception cref="ArgumentException">Thrown when an attempt is made to set
61-
/// the value to a time span of less tnan or equal to zero milliseconds.</exception>
53+
/// the value to a time span of less than or equal to zero.</exception>
6254
public TimeSpan BufferUsageReportingInterval
6355
{
64-
get
65-
{
66-
return this.bufferUsageReportingInterval;
67-
}
56+
get => this.bufferUsageReportingInterval;
6857

6958
set
7059
{
71-
if (value.TotalMilliseconds <= 0)
60+
if (value <= TimeSpan.Zero)
7261
{
73-
throw new ArgumentException("Interval must be greater than zero.");
62+
throw new ArgumentException("Interval must be greater than zero.", nameof(value));
7463
}
7564

7665
this.bufferUsageReportingInterval = value;
@@ -80,23 +69,13 @@ public TimeSpan BufferUsageReportingInterval
8069
/// <summary>
8170
/// Gets a comma-separated list of the categories for which tracing is enabled.
8271
/// </summary>
83-
public string TracingCategories
84-
{
85-
get
86-
{
87-
if (this.tracingCategories.Count == 0)
88-
{
89-
return string.Empty;
90-
}
91-
92-
return string.Join(",", this.tracingCategories.ToArray());
93-
}
94-
}
72+
public string TracingCategories => string.Join(",", this.tracingCategories);
9573

9674
/// <summary>
9775
/// Adds a single category to the list of Chromium tracing categories for which events should be collected.
9876
/// </summary>
9977
/// <param name="category">The category to add.</param>
78+
/// <exception cref="ArgumentException">If <paramref name="category"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
10079
public void AddTracingCategory(string category)
10180
{
10281
if (string.IsNullOrEmpty(category))
@@ -111,15 +90,17 @@ public void AddTracingCategory(string category)
11190
/// Adds categories to the list of Chromium tracing categories for which events should be collected.
11291
/// </summary>
11392
/// <param name="categoriesToAdd">An array of categories to add.</param>
93+
/// <exception cref="ArgumentNullException">If <paramref name="categoriesToAdd"/> is <see langword="null"/>.</exception>
11494
public void AddTracingCategories(params string[] categoriesToAdd)
11595
{
116-
this.AddTracingCategories(new List<string>(categoriesToAdd));
96+
this.AddTracingCategories((IEnumerable<string>)categoriesToAdd);
11797
}
11898

11999
/// <summary>
120100
/// Adds categories to the list of Chromium tracing categories for which events should be collected.
121101
/// </summary>
122102
/// <param name="categoriesToAdd">An <see cref="IEnumerable{T}"/> object of categories to add.</param>
103+
/// <exception cref="ArgumentNullException">If <paramref name="categoriesToAdd"/> is <see langword="null"/>.</exception>
123104
public void AddTracingCategories(IEnumerable<string> categoriesToAdd)
124105
{
125106
if (categoriesToAdd == null)

0 commit comments

Comments
 (0)