Skip to content

Commit

Permalink
Further locking down .NET capability manipulation by using ReadOnlyDi…
Browse files Browse the repository at this point in the history
…ctionary
  • Loading branch information
jimevans committed Apr 11, 2019
1 parent c48ea4b commit c9bc53b
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Chrome/ChromeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class ChromeOptions : DriverOptions
{
/// <summary>
/// Gets the name of the capability used to store Chrome options in
/// a <see cref="DesiredCapabilities"/> object.
/// an <see cref="ICapabilities"/> object.
/// </summary>
public static readonly string Capability = "goog:chromeOptions";

Expand Down
7 changes: 6 additions & 1 deletion dotnet/src/webdriver/DriverOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,14 @@ public override string ToString()
/// Returns the current options as a <see cref="Dictionary{TKey, TValue}"/>.
/// </summary>
/// <returns>The current options as a <see cref="Dictionary{TKey, TValue}"/>.</returns>
internal Dictionary<string, object> ToDictionary()
internal IDictionary<string, object> ToDictionary()
{
IHasCapabilitiesDictionary desired = this.ToCapabilities() as IHasCapabilitiesDictionary;
if (desired == null)
{
return null;
}

return desired.CapabilitiesDictionary;
}

Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/IE/InternetExplorerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class InternetExplorerOptions : DriverOptions
{
/// <summary>
/// Gets the name of the capability used to store IE options in
/// a <see cref="DesiredCapabilities"/> object.
/// an <see cref="ICapabilities"/> object.
/// </summary>
public static readonly string Capability = "se:ieOptions";

Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/webdriver/Internal/IHasCapabilitiesDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace OpenQA.Selenium.Internal
internal interface IHasCapabilitiesDictionary
{
/// <summary>
/// Gets the underlying Dictionary for a given set of capabilities.
/// Gets the underlying IDictionary for a given set of capabilities.
/// </summary>
Dictionary<string, object> CapabilitiesDictionary { get; }
IDictionary<string, object> CapabilitiesDictionary { get; }
}
}
7 changes: 4 additions & 3 deletions dotnet/src/webdriver/Internal/ReturnedCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using Newtonsoft.Json;
using OpenQA.Selenium.Remote;
Expand Down Expand Up @@ -96,17 +97,17 @@ public object this[string capabilityName]
/// <summary>
/// Gets the underlying Dictionary for a given set of capabilities.
/// </summary>
Dictionary<string, object> IHasCapabilitiesDictionary.CapabilitiesDictionary
IDictionary<string, object> IHasCapabilitiesDictionary.CapabilitiesDictionary
{
get { return this.CapabilitiesDictionary; }
}

/// <summary>
/// Gets the internal capabilities dictionary.
/// </summary>
internal Dictionary<string, object> CapabilitiesDictionary
internal IDictionary<string, object> CapabilitiesDictionary
{
get { return this.capabilities; }
get { return new ReadOnlyDictionary<string, object>(this.capabilities); }
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Opera/OperaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class OperaOptions : DriverOptions
{
/// <summary>
/// Gets the name of the capability used to store Opera options in
/// a <see cref="DesiredCapabilities"/> object.
/// an <see cref="ICapabilities"/> object.
/// </summary>
public static readonly string Capability = "operaOptions";

Expand Down
10 changes: 5 additions & 5 deletions dotnet/src/webdriver/Remote/DesiredCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using OpenQA.Selenium.Internal;

namespace OpenQA.Selenium.Remote
{
/// <summary>
/// Class to Create the capabilities of the browser you require for <see cref="IWebDriver"/>.
/// If you wish to use default values use the static methods
/// Internal class to specify the requested capabilities of the browser for <see cref="IWebDriver"/>.
/// </summary>
[Obsolete("Use of DesiredCapabilities has been deprecated in favor of browser-specific Options classes")]
internal class DesiredCapabilities : IWritableCapabilities, IHasCapabilitiesDictionary
Expand Down Expand Up @@ -182,17 +182,17 @@ public bool AcceptInsecureCerts
/// <summary>
/// Gets the underlying Dictionary for a given set of capabilities.
/// </summary>
Dictionary<string, object> IHasCapabilitiesDictionary.CapabilitiesDictionary
IDictionary<string, object> IHasCapabilitiesDictionary.CapabilitiesDictionary
{
get { return this.CapabilitiesDictionary; }
}

/// <summary>
/// Gets the underlying Dictionary for a given set of capabilities.
/// </summary>
internal Dictionary<string, object> CapabilitiesDictionary
internal IDictionary<string, object> CapabilitiesDictionary
{
get { return this.capabilities; }
get { return new ReadOnlyDictionary<string, object>(this.capabilities); }
}

/// <summary>
Expand Down
19 changes: 9 additions & 10 deletions dotnet/src/webdriver/Remote/ReadOnlyDesiredCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using OpenQA.Selenium.Internal;

Expand All @@ -40,7 +41,7 @@ private ReadOnlyDesiredCapabilities()

internal ReadOnlyDesiredCapabilities(DesiredCapabilities desiredCapabilities)
{
Dictionary<string, object> internalDictionary = desiredCapabilities.CapabilitiesDictionary;
IDictionary<string, object> internalDictionary = desiredCapabilities.CapabilitiesDictionary;
foreach(KeyValuePair<string, object> keyValuePair in internalDictionary)
{
this.capabilities[keyValuePair.Key] = keyValuePair.Value;
Expand Down Expand Up @@ -115,17 +116,17 @@ public bool AcceptInsecureCerts
/// <summary>
/// Gets the underlying Dictionary for a given set of capabilities.
/// </summary>
Dictionary<string, object> IHasCapabilitiesDictionary.CapabilitiesDictionary
IDictionary<string, object> IHasCapabilitiesDictionary.CapabilitiesDictionary
{
get { return this.CapabilitiesDictionary; }
}

/// <summary>
/// Gets the underlying Dictionary for a given set of capabilities.
/// </summary>
internal Dictionary<string, object> CapabilitiesDictionary
internal IDictionary<string, object> CapabilitiesDictionary
{
get { return this.capabilities; }
get { return new ReadOnlyDictionary<string, object>(this.capabilities); }
}

/// <summary>
Expand Down Expand Up @@ -182,14 +183,12 @@ public object GetCapability(string capability)
}

/// <summary>
/// Converts the <see cref="ICapabilities"/> object to a <see cref="Dictionary{TKey, TValue}"/>.
/// Converts the <see cref="ICapabilities"/> object to a read-only <see cref="IDictionary{TKey, TValue}"/>.
/// </summary>
/// <returns>The <see cref="Dictionary{TKey, TValue}"/> containing the capabilities.</returns>
public Dictionary<string, object> ToDictionary()
/// <returns>A read-only <see cref="IDictionary{TKey, TValue}"/> containing the capabilities.</returns>
public IDictionary<string, object> ToDictionary()
{
// CONSIDER: Instead of returning the raw internal member,
// we might want to copy/clone it instead.
return this.capabilities;
return new ReadOnlyDictionary<string, object>(this.capabilities);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Remote/RemoteSessionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ internal DriverOptions GetFirstMatchDriverOptions(int firstMatchIndex)
return this.firstMatchOptions[firstMatchIndex];
}

private Dictionary<string, object> GetAlwaysMatchOptionsAsSerializableDictionary()
private IDictionary<string, object> GetAlwaysMatchOptionsAsSerializableDictionary()
{
return this.mustMatchDriverOptions.ToDictionary();
}
Expand Down

0 comments on commit c9bc53b

Please sign in to comment.