Skip to content

Commit

Permalink
[dotnet] Add nullability to Chromium configuration types (#15204)
Browse files Browse the repository at this point in the history
  • Loading branch information
RenderMichael authored Jan 31, 2025
1 parent 5bad332 commit 4666186
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 110 deletions.
17 changes: 4 additions & 13 deletions dotnet/src/webdriver/Chromium/ChromiumAndroidOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@

using OpenQA.Selenium.Internal;

#nullable enable

namespace OpenQA.Selenium.Chromium
{
/// <summary>
/// Generates the capabilities for automating Chromium applications on Android
/// </summary>
public class ChromiumAndroidOptions : AndroidOptions
{
private string androidProcess;
private bool androidUseRunningApp;

/// <summary>
/// Initializes a new instance of the <see cref="ChromiumAndroidOptions"/> class.
/// </summary>
Expand All @@ -40,19 +39,11 @@ public ChromiumAndroidOptions(string androidPackage) : base(androidPackage)
/// <summary>
/// Gets or sets a value indicating whether to use an already running app.
/// </summary>
public bool UseRunningApp
{
get { return this.androidUseRunningApp; }
set { this.androidUseRunningApp = value; }
}
public bool UseRunningApp { get; set; }

/// <summary>
/// Gets or sets the process name of the Activity hosting the app.
/// </summary>
public string AndroidProcess
{
get { return this.androidProcess; }
set { this.androidProcess = value; }
}
public string? AndroidProcess { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium.Chromium
{
/// <summary>
Expand All @@ -25,12 +27,6 @@ namespace OpenQA.Selenium.Chromium
/// </summary>
public class ChromiumMobileEmulationDeviceSettings
{
private string userAgent = string.Empty;
private long width;
private long height;
private double pixelRatio;
private bool enableTouchEvents = true;

/// <summary>
/// Initializes a new instance of the <see cref="ChromiumMobileEmulationDeviceSettings"/> class.
/// </summary>
Expand All @@ -43,59 +39,39 @@ public ChromiumMobileEmulationDeviceSettings()
/// </summary>
/// <param name="userAgent">The user agent string to be used by the browser when emulating
/// a mobile device.</param>
public ChromiumMobileEmulationDeviceSettings(string userAgent)
public ChromiumMobileEmulationDeviceSettings(string? userAgent)
{
this.userAgent = userAgent;
this.UserAgent = userAgent;
}

/// <summary>
/// Gets or sets the user agent string to be used by the browser when emulating
/// a mobile device.
/// </summary>
public string UserAgent
{
get { return this.userAgent; }
set { this.userAgent = value; }
}
public string? UserAgent { get; set; }

/// <summary>
/// Gets or sets the width in pixels to be used by the browser when emulating
/// a mobile device.
/// </summary>
public long Width
{
get { return this.width; }
set { this.width = value; }
}
public long Width { get; set; }

/// <summary>
/// Gets or sets the height in pixels to be used by the browser when emulating
/// a mobile device.
/// </summary>
public long Height
{
get { return this.height; }
set { this.height = value; }
}
public long Height { get; set; }

/// <summary>
/// Gets or sets the pixel ratio to be used by the browser when emulating
/// a mobile device.
/// </summary>
public double PixelRatio
{
get { return this.pixelRatio; }
set { this.pixelRatio = value; }
}
public double PixelRatio { get; set; }

/// <summary>
/// Gets or sets a value indicating whether touch events should be enabled by
/// the browser when emulating a mobile device. Defaults to <see langword="true"/>.
/// </summary>
public bool EnableTouchEvents
{
get { return this.enableTouchEvents; }
set { this.enableTouchEvents = value; }
}
public bool EnableTouchEvents { get; set; } = true;
}
}
49 changes: 18 additions & 31 deletions dotnet/src/webdriver/Chromium/ChromiumNetworkConditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,42 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

#nullable enable

namespace OpenQA.Selenium.Chromium
{
/// <summary>
/// Provides manipulation of getting and setting network conditions from Chromium.
/// </summary>
public class ChromiumNetworkConditions
{
private bool offline;
private TimeSpan latency = TimeSpan.Zero;
private long downloadThroughput = 0;
private long uploadThroughput = 0;

/// <summary>
/// Gets or sets a value indicating whether the network is offline. Defaults to <see langword="false"/>.
/// </summary>
[JsonPropertyName("offline")]
public bool IsOffline
{
get { return this.offline; }
set { this.offline = value; }
}
public bool IsOffline { get; set; }

/// <summary>
/// Gets or sets the simulated latency of the connection. Typically given in milliseconds.
/// </summary>
[JsonIgnore]
public TimeSpan Latency
{
get { return this.latency; }
set { this.latency = value; }
}
public TimeSpan Latency { get; set; } = TimeSpan.Zero;

/// <summary>
/// Gets or sets the throughput of the network connection in bytes/second for downloading.
/// </summary>
[JsonPropertyName("download_throughput")]
public long DownloadThroughput
{
get { return this.downloadThroughput; }
get => this.downloadThroughput;
set
{
if (value < 0)
{
throw new WebDriverException("Downlod throughput cannot be negative.");
throw new WebDriverException("Download throughput cannot be negative.");
}

this.downloadThroughput = value;
Expand All @@ -77,7 +69,7 @@ public long DownloadThroughput
[JsonPropertyName("upload_throughput")]
public long UploadThroughput
{
get { return this.uploadThroughput; }
get => this.uploadThroughput;
set
{
if (value < 0)
Expand All @@ -92,13 +84,7 @@ public long UploadThroughput
[JsonPropertyName("latency")]
[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
internal long? SerializableLatency
{
get
{
return Convert.ToInt64(this.latency.TotalMilliseconds);
}
}
internal long? SerializableLatency => Convert.ToInt64(this.Latency.TotalMilliseconds);

/// <summary>
/// Creates a ChromiumNetworkConditions object from a dictionary of key-value pairs.
Expand All @@ -108,24 +94,24 @@ internal long? SerializableLatency
public static ChromiumNetworkConditions FromDictionary(Dictionary<string, object> dictionary)
{
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
if (dictionary.ContainsKey("offline"))
if (dictionary.TryGetValue("offline", out object? offline))
{
conditions.IsOffline = (bool)dictionary["offline"];
conditions.IsOffline = (bool)offline;
}

if (dictionary.ContainsKey("latency"))
if (dictionary.TryGetValue("latency", out object? latency))
{
conditions.Latency = TimeSpan.FromMilliseconds(Convert.ToDouble(dictionary["latency"]));
conditions.Latency = TimeSpan.FromMilliseconds(Convert.ToDouble(latency));
}

if (dictionary.ContainsKey("upload_throughput"))
if (dictionary.TryGetValue("upload_throughput", out object? uploadThroughput))
{
conditions.UploadThroughput = (long)dictionary["upload_throughput"];
conditions.UploadThroughput = (long)uploadThroughput;
}

if (dictionary.ContainsKey("download_throughput"))
if (dictionary.TryGetValue("download_throughput", out object? downloadThroughput))
{
conditions.DownloadThroughput = (long)dictionary["download_throughput"];
conditions.DownloadThroughput = (long)downloadThroughput;
}

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

this.uploadThroughput = throughput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using System;
using System.Collections.Generic;

#nullable enable

namespace OpenQA.Selenium.Chromium
{
/// <summary>
Expand All @@ -28,49 +30,36 @@ namespace OpenQA.Selenium.Chromium
/// </summary>
public class ChromiumPerformanceLoggingPreferences
{
private bool isCollectingNetworkEvents = true;
private bool isCollectingPageEvents = true;
private TimeSpan bufferUsageReportingInterval = TimeSpan.FromMilliseconds(1000);
private List<string> tracingCategories = new List<string>();
private readonly List<string> tracingCategories = new List<string>();

/// <summary>
/// Gets or sets a value indicating whether Chromium will collect events from the Network domain.
/// Defaults to <see langword="true"/>.
/// </summary>
public bool IsCollectingNetworkEvents
{
get { return this.isCollectingNetworkEvents; }
set { this.isCollectingNetworkEvents = value; }
}
public bool IsCollectingNetworkEvents { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether Chromium will collect events from the Page domain.
/// Defaults to <see langword="true"/>.
/// </summary>
public bool IsCollectingPageEvents
{
get { return this.isCollectingPageEvents; }
set { this.isCollectingPageEvents = value; }
}
public bool IsCollectingPageEvents { get; set; } = true;

/// <summary>
/// Gets or sets the interval between Chromium DevTools trace buffer usage events.
/// Defaults to 1000 milliseconds.
/// </summary>
/// <exception cref="ArgumentException">Thrown when an attempt is made to set
/// the value to a time span of less tnan or equal to zero milliseconds.</exception>
/// the value to a time span of less than or equal to zero.</exception>
public TimeSpan BufferUsageReportingInterval
{
get
{
return this.bufferUsageReportingInterval;
}
get => this.bufferUsageReportingInterval;

set
{
if (value.TotalMilliseconds <= 0)
if (value <= TimeSpan.Zero)
{
throw new ArgumentException("Interval must be greater than zero.");
throw new ArgumentException("Interval must be greater than zero.", nameof(value));
}

this.bufferUsageReportingInterval = value;
Expand All @@ -80,23 +69,13 @@ public TimeSpan BufferUsageReportingInterval
/// <summary>
/// Gets a comma-separated list of the categories for which tracing is enabled.
/// </summary>
public string TracingCategories
{
get
{
if (this.tracingCategories.Count == 0)
{
return string.Empty;
}

return string.Join(",", this.tracingCategories.ToArray());
}
}
public string TracingCategories => string.Join(",", this.tracingCategories);

/// <summary>
/// Adds a single category to the list of Chromium tracing categories for which events should be collected.
/// </summary>
/// <param name="category">The category to add.</param>
/// <exception cref="ArgumentException">If <paramref name="category"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
public void AddTracingCategory(string category)
{
if (string.IsNullOrEmpty(category))
Expand All @@ -111,15 +90,17 @@ public void AddTracingCategory(string category)
/// Adds categories to the list of Chromium tracing categories for which events should be collected.
/// </summary>
/// <param name="categoriesToAdd">An array of categories to add.</param>
/// <exception cref="ArgumentNullException">If <paramref name="categoriesToAdd"/> is <see langword="null"/>.</exception>
public void AddTracingCategories(params string[] categoriesToAdd)
{
this.AddTracingCategories(new List<string>(categoriesToAdd));
this.AddTracingCategories((IEnumerable<string>)categoriesToAdd);
}

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

0 comments on commit 4666186

Please sign in to comment.