Skip to content

Commit 0157a05

Browse files
authored
[dotnet] [bidi] Align SetGeolocation polymorphic command (#17338)
1 parent 82fd32a commit 0157a05

4 files changed

Lines changed: 30 additions & 32 deletions

File tree

dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,20 @@ public async Task<SetScrollbarTypeOverrideResult> SetScrollbarTypeOverrideAsync(
105105
return await ExecuteAsync(SetScrollbarTypeOverrideCommand, @params, options, cancellationToken).ConfigureAwait(false);
106106
}
107107

108-
public async Task<SetGeolocationOverrideResult> SetGeolocationCoordinatesOverrideAsync(double latitude, double longitude, SetGeolocationCoordinatesOverrideOptions? options = null, CancellationToken cancellationToken = default)
108+
public async Task<SetGeolocationOverrideResult> SetGeolocationOverrideAsync(GeolocationOverride? geolocationOverride, SetGeolocationOverrideOptions? options = null, CancellationToken cancellationToken = default)
109109
{
110-
var coordinates = new GeolocationCoordinates(latitude, longitude, options?.Accuracy, options?.Altitude, options?.AltitudeAccuracy, options?.Heading, options?.Speed);
111-
var @params = new SetGeolocationOverrideCoordinatesParameters(coordinates, options?.Contexts, options?.UserContexts);
112-
return await ExecuteAsync(SetGeolocationOverrideCommand, @params, options, cancellationToken).ConfigureAwait(false);
113-
}
110+
SetGeolocationOverrideParameters @params = geolocationOverride switch
111+
{
112+
GeolocationCoordinatesOverride c => new SetGeolocationOverrideCoordinatesParameters(
113+
new GeolocationCoordinates(c.Latitude, c.Longitude, c.Accuracy, c.Altitude, c.AltitudeAccuracy, c.Heading, c.Speed),
114+
options?.Contexts, options?.UserContexts),
115+
GeolocationPositionErrorOverride => new SetGeolocationOverridePositionErrorParameters(
116+
new GeolocationPositionError(), options?.Contexts, options?.UserContexts),
117+
null => new SetGeolocationOverrideCoordinatesParameters(
118+
null, options?.Contexts, options?.UserContexts),
119+
_ => throw new ArgumentException($"Unknown geolocation override type: {geolocationOverride.GetType()}", nameof(geolocationOverride))
120+
};
114121

115-
public async Task<SetGeolocationOverrideResult> SetGeolocationCoordinatesOverrideAsync(SetGeolocationOverrideOptions? options = null, CancellationToken cancellationToken = default)
116-
{
117-
var @params = new SetGeolocationOverrideCoordinatesParameters(null, options?.Contexts, options?.UserContexts);
118-
return await ExecuteAsync(SetGeolocationOverrideCommand, @params, options, cancellationToken).ConfigureAwait(false);
119-
}
120-
121-
public async Task<SetGeolocationOverrideResult> SetGeolocationPositionErrorOverrideAsync(SetGeolocationPositionErrorOverrideOptions? options = null, CancellationToken cancellationToken = default)
122-
{
123-
var @params = new SetGeolocationOverridePositionErrorParameters(new GeolocationPositionError(), options?.Contexts, options?.UserContexts);
124122
return await ExecuteAsync(SetGeolocationOverrideCommand, @params, options, cancellationToken).ConfigureAwait(false);
125123
}
126124

dotnet/src/webdriver/BiDi/Emulation/IEmulationModule.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ namespace OpenQA.Selenium.BiDi.Emulation;
2222
public interface IEmulationModule
2323
{
2424
Task<SetForcedColorsModeThemeOverrideResult> SetForcedColorsModeThemeOverrideAsync(ForcedColorsModeTheme? theme, SetForcedColorsModeThemeOverrideOptions? options = null, CancellationToken cancellationToken = default);
25-
Task<SetGeolocationOverrideResult> SetGeolocationCoordinatesOverrideAsync(double latitude, double longitude, SetGeolocationCoordinatesOverrideOptions? options = null, CancellationToken cancellationToken = default);
26-
Task<SetGeolocationOverrideResult> SetGeolocationCoordinatesOverrideAsync(SetGeolocationOverrideOptions? options = null, CancellationToken cancellationToken = default);
27-
Task<SetGeolocationOverrideResult> SetGeolocationPositionErrorOverrideAsync(SetGeolocationPositionErrorOverrideOptions? options = null, CancellationToken cancellationToken = default);
25+
Task<SetGeolocationOverrideResult> SetGeolocationOverrideAsync(GeolocationOverride? geolocationOverride, SetGeolocationOverrideOptions? options = null, CancellationToken cancellationToken = default);
2826
Task<SetLocaleOverrideResult> SetLocaleOverrideAsync(string? locale, SetLocaleOverrideOptions? options = null, CancellationToken cancellationToken = default);
2927
Task<SetNetworkConditionsResult> SetNetworkConditionsAsync(NetworkConditions? networkConditions, SetNetworkConditionsOptions? options = null, CancellationToken cancellationToken = default);
3028
Task<SetScreenOrientationOverrideResult> SetScreenOrientationOverrideAsync(ScreenOrientation? screenOrientation, SetScreenOrientationOverrideOptions? options = null, CancellationToken cancellationToken = default);

dotnet/src/webdriver/BiDi/Emulation/SetGeolocationOverride.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@
2121

2222
namespace OpenQA.Selenium.BiDi.Emulation;
2323

24+
public abstract record GeolocationOverride;
25+
26+
public sealed record GeolocationCoordinatesOverride(double Latitude, double Longitude) : GeolocationOverride
27+
{
28+
public double? Accuracy { get; init; }
29+
public double? Altitude { get; init; }
30+
public double? AltitudeAccuracy { get; init; }
31+
public double? Heading { get; init; }
32+
public double? Speed { get; init; }
33+
}
34+
35+
public sealed record GeolocationPositionErrorOverride : GeolocationOverride;
36+
2437
[JsonDerivedType(typeof(SetGeolocationOverrideCoordinatesParameters))]
2538
[JsonDerivedType(typeof(SetGeolocationOverridePositionErrorParameters))]
2639
internal abstract record SetGeolocationOverrideParameters(IEnumerable<BrowsingContext.BrowsingContext>? Contexts, IEnumerable<Browser.UserContext>? UserContexts) : Parameters;
@@ -37,22 +50,11 @@ internal sealed record GeolocationPositionError
3750
internal string Type { get; } = "positionUnavailable";
3851
}
3952

40-
public record SetGeolocationOverrideOptions : CommandOptions
53+
public sealed record SetGeolocationOverrideOptions : CommandOptions
4154
{
4255
public IEnumerable<BrowsingContext.BrowsingContext>? Contexts { get; init; }
4356

4457
public IEnumerable<Browser.UserContext>? UserContexts { get; init; }
4558
}
4659

47-
public sealed record SetGeolocationCoordinatesOverrideOptions : SetGeolocationOverrideOptions
48-
{
49-
public double? Accuracy { get; init; }
50-
public double? Altitude { get; init; }
51-
public double? AltitudeAccuracy { get; init; }
52-
public double? Heading { get; init; }
53-
public double? Speed { get; init; }
54-
}
55-
56-
public sealed record SetGeolocationPositionErrorOverrideOptions : SetGeolocationOverrideOptions;
57-
5860
public sealed record SetGeolocationOverrideResult : EmptyResult;

dotnet/test/webdriver/BiDi/Emulation/EmulationTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void CanSetGeolocationCoordinatesOverride()
198198
{
199199
Assert.That(async () =>
200200
{
201-
await bidi.Emulation.SetGeolocationCoordinatesOverrideAsync(0, 0, new() { Contexts = [context] });
201+
await bidi.Emulation.SetGeolocationOverrideAsync(new GeolocationCoordinatesOverride(0, 0), new() { Contexts = [context] });
202202
},
203203
Throws.Nothing);
204204
}
@@ -208,7 +208,7 @@ public void CanSetGeolocationCoordinatesOverrideToDefault()
208208
{
209209
Assert.That(async () =>
210210
{
211-
await bidi.Emulation.SetGeolocationCoordinatesOverrideAsync(new() { Contexts = [context] });
211+
await bidi.Emulation.SetGeolocationOverrideAsync(null, new() { Contexts = [context] });
212212
},
213213
Throws.Nothing);
214214
}
@@ -219,7 +219,7 @@ public void CanSetGeolocationPositionErrorOverride()
219219
{
220220
Assert.That(async () =>
221221
{
222-
await bidi.Emulation.SetGeolocationPositionErrorOverrideAsync(new() { Contexts = [context] });
222+
await bidi.Emulation.SetGeolocationOverrideAsync(new GeolocationPositionErrorOverride(), new() { Contexts = [context] });
223223
},
224224
Throws.Nothing);
225225
}

0 commit comments

Comments
 (0)