Skip to content

Commit 1e75ae9

Browse files
committed
SetGeolocation
1 parent c811212 commit 1e75ae9

File tree

4 files changed

+106
-12
lines changed

4 files changed

+106
-12
lines changed

dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,6 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
178178
[JsonSerializable(typeof(Emulation.SetForcedColorsModeThemeOverrideCommand))]
179179
[JsonSerializable(typeof(Emulation.SetScriptingEnabledCommand))]
180180
[JsonSerializable(typeof(Emulation.SetScreenOrientationOverrideCommand))]
181+
[JsonSerializable(typeof(Emulation.SetGeolocationOverrideCommand))]
181182

182183
internal partial class BiDiJsonSerializerContext : JsonSerializerContext;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using System;
2021
using System.Threading.Tasks;
2122
using OpenQA.Selenium.BiDi.Communication;
2223

@@ -65,4 +66,27 @@ public async Task<EmptyResult> SetScreenOrientationOverrideAsync(ScreenOrientati
6566

6667
return await Broker.ExecuteCommandAsync<SetScreenOrientationOverrideCommand, EmptyResult>(new SetScreenOrientationOverrideCommand(@params), options).ConfigureAwait(false);
6768
}
69+
70+
public async Task<EmptyResult> SetGeolocationCoordinatesOverrideAsync(double latitude, double longitude, SetGeolocationCoordinatesOverrideOptions? options = null)
71+
{
72+
var coordinates = new GeolocationCoordinates(latitude, longitude, options?.Accuracy, options?.Altitude, options?.AltitudeAccuracy, options?.Heading, options?.Speed);
73+
74+
var @params = new SetGeolocationOverrideCoordinatesParameters(coordinates, options?.Contexts, options?.UserContexts);
75+
76+
return await Broker.ExecuteCommandAsync<SetGeolocationOverrideCommand, EmptyResult>(new SetGeolocationOverrideCommand(@params), options).ConfigureAwait(false);
77+
}
78+
79+
public async Task<EmptyResult> SetGeolocationCoordinatesOverrideAsync(SetGeolocationOverrideOptions? options = null)
80+
{
81+
var @params = new SetGeolocationOverrideCoordinatesParameters(null, options?.Contexts, options?.UserContexts);
82+
83+
return await Broker.ExecuteCommandAsync<SetGeolocationOverrideCommand, EmptyResult>(new SetGeolocationOverrideCommand(@params), options).ConfigureAwait(false);
84+
}
85+
86+
public async Task<EmptyResult> SetGeolocationPositionErrorOverrideAsync(SetGeolocationPositionErrorOverrideOptions? options = null)
87+
{
88+
var @params = new SetGeolocationOverridePositionErrorParameters(new GeolocationPositionError(), options?.Contexts, options?.UserContexts);
89+
90+
return await Broker.ExecuteCommandAsync<SetGeolocationOverrideCommand, EmptyResult>(new SetGeolocationOverrideCommand(@params), options).ConfigureAwait(false);
91+
}
6892
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// <copyright file="SetGeolocationOverrideCommand.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using System.Collections.Generic;
21+
using System.Text.Json.Serialization;
22+
using OpenQA.Selenium.BiDi.Communication;
23+
24+
namespace OpenQA.Selenium.BiDi.Emulation;
25+
26+
internal sealed class SetGeolocationOverrideCommand(SetGeolocationOverrideParameters @params)
27+
: Command<SetGeolocationOverrideParameters, EmptyResult>(@params, "emulation.setGeolocationOverride");
28+
29+
[JsonDerivedType(typeof(SetGeolocationOverrideCoordinatesParameters))]
30+
[JsonDerivedType(typeof(SetGeolocationOverridePositionErrorParameters))]
31+
internal abstract record SetGeolocationOverrideParameters(IEnumerable<BrowsingContext.BrowsingContext>? Contexts, IEnumerable<Browser.UserContext>? UserContexts) : Parameters;
32+
33+
internal sealed record SetGeolocationOverrideCoordinatesParameters([property: JsonIgnore(Condition = JsonIgnoreCondition.Never)] GeolocationCoordinates? Coordinates, IEnumerable<BrowsingContext.BrowsingContext>? Contexts, IEnumerable<Browser.UserContext>? UserContexts) : SetGeolocationOverrideParameters(Contexts, UserContexts);
34+
35+
internal sealed record SetGeolocationOverridePositionErrorParameters(GeolocationPositionError Error, IEnumerable<BrowsingContext.BrowsingContext>? Contexts, IEnumerable<Browser.UserContext>? UserContexts) : SetGeolocationOverrideParameters(Contexts, UserContexts);
36+
37+
internal sealed record GeolocationCoordinates(double Latitude, double Longitude, double? Accuracy, double? Altitude, double? AltitudeAccuracy, double? Heading, double? Speed);
38+
39+
internal sealed record GeolocationPositionError
40+
{
41+
[JsonInclude]
42+
internal string Type { get; } = "positionUnavailable";
43+
}
44+
45+
public class SetGeolocationOverrideOptions : CommandOptions
46+
{
47+
public IEnumerable<BrowsingContext.BrowsingContext>? Contexts { get; set; }
48+
49+
public IEnumerable<Browser.UserContext>? UserContexts { get; set; }
50+
}
51+
52+
public sealed class SetGeolocationCoordinatesOverrideOptions : SetGeolocationOverrideOptions
53+
{
54+
public double? Accuracy { get; set; }
55+
public double? Altitude { get; set; }
56+
public double? AltitudeAccuracy { get; set; }
57+
public double? Heading { get; set; }
58+
public double? Speed { get; set; }
59+
}
60+
61+
public sealed class SetGeolocationPositionErrorOverrideOptions : SetGeolocationOverrideOptions;

dotnet/test/common/BiDi/Emulation/EmulationTest.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,51 @@ class EmulationTest : BiDiTestFixture
2626
[Test]
2727
public void CanSetTimezoneOverride()
2828
{
29-
Assert.That(async () => await bidi.Emulation.SetTimezoneOverrideAsync("UTC", new () { Contexts = [context] }), Throws.Nothing);
30-
Assert.That(async () => await bidi.Emulation.SetTimezoneOverrideAsync(null, new () { Contexts = [context] }), Throws.Nothing);
29+
Assert.That(async () => await bidi.Emulation.SetTimezoneOverrideAsync("UTC", new() { Contexts = [context] }), Throws.Nothing);
30+
Assert.That(async () => await bidi.Emulation.SetTimezoneOverrideAsync(null, new() { Contexts = [context] }), Throws.Nothing);
3131
}
3232

3333
[Test]
3434
public void CanSetUserAgentOverride()
3535
{
36-
Assert.That(async () => await bidi.Emulation.SetUserAgentOverrideAsync("MyUserAgent/1.0", new () { Contexts = [context] }), Throws.Nothing);
37-
Assert.That(async () => await bidi.Emulation.SetUserAgentOverrideAsync(null, new () { Contexts = [context] }), Throws.Nothing);
36+
Assert.That(async () => await bidi.Emulation.SetUserAgentOverrideAsync("MyUserAgent/1.0", new() { Contexts = [context] }), Throws.Nothing);
37+
Assert.That(async () => await bidi.Emulation.SetUserAgentOverrideAsync(null, new() { Contexts = [context] }), Throws.Nothing);
3838
}
3939

4040
[Test]
4141
public void CanSetLocaleOverride()
4242
{
43-
Assert.That(async () => await bidi.Emulation.SetLocaleOverrideAsync("en-US", new () { Contexts = [context] }), Throws.Nothing);
44-
Assert.That(async () => await bidi.Emulation.SetLocaleOverrideAsync(null, new () { Contexts = [context] }), Throws.Nothing);
43+
Assert.That(async () => await bidi.Emulation.SetLocaleOverrideAsync("en-US", new() { Contexts = [context] }), Throws.Nothing);
44+
Assert.That(async () => await bidi.Emulation.SetLocaleOverrideAsync(null, new() { Contexts = [context] }), Throws.Nothing);
4545
}
4646

4747
[Test]
4848
public void CanSetForcedColorsModeThemeOverride()
4949
{
50-
Assert.That(async () => await bidi.Emulation.SetForcedColorsModeThemeOverrideAsync(ForcedColorsModeTheme.Light, new () { Contexts = [context] }), Throws.Nothing);
51-
Assert.That(async () => await bidi.Emulation.SetForcedColorsModeThemeOverrideAsync(null, new () { Contexts = [context] }), Throws.Nothing);
50+
Assert.That(async () => await bidi.Emulation.SetForcedColorsModeThemeOverrideAsync(ForcedColorsModeTheme.Light, new() { Contexts = [context] }), Throws.Nothing);
51+
Assert.That(async () => await bidi.Emulation.SetForcedColorsModeThemeOverrideAsync(null, new() { Contexts = [context] }), Throws.Nothing);
5252
}
5353

5454
[Test]
5555
public void CanSetScriptingEnabled()
5656
{
57-
Assert.That(async () => await bidi.Emulation.SetScriptingEnabledAsync(false, new () { Contexts = [context] }), Throws.Nothing);
58-
Assert.That(async () => await bidi.Emulation.SetScriptingEnabledAsync(null, new () { Contexts = [context] }), Throws.Nothing);
57+
Assert.That(async () => await bidi.Emulation.SetScriptingEnabledAsync(false, new() { Contexts = [context] }), Throws.Nothing);
58+
Assert.That(async () => await bidi.Emulation.SetScriptingEnabledAsync(null, new() { Contexts = [context] }), Throws.Nothing);
5959
}
6060

6161
[Test]
6262
public void CanSetScreenOrientationOverride()
6363
{
6464
var orientation = new ScreenOrientation(ScreenOrientationNatural.Portrait, ScreenOrientationType.PortraitPrimary);
65-
Assert.That(async () => await bidi.Emulation.SetScreenOrientationOverrideAsync(orientation, new () { Contexts = [context] }), Throws.Nothing);
66-
Assert.That(async () => await bidi.Emulation.SetScreenOrientationOverrideAsync(null, new () { Contexts = [context] }), Throws.Nothing);
65+
Assert.That(async () => await bidi.Emulation.SetScreenOrientationOverrideAsync(orientation, new() { Contexts = [context] }), Throws.Nothing);
66+
Assert.That(async () => await bidi.Emulation.SetScreenOrientationOverrideAsync(null, new() { Contexts = [context] }), Throws.Nothing);
67+
}
68+
69+
[Test]
70+
public void CanSetGeolocationOverride()
71+
{
72+
Assert.That(async () => await bidi.Emulation.SetGeolocationCoordinatesOverrideAsync(0, 0, new() { Contexts = [context] }), Throws.Nothing);
73+
Assert.That(async () => await bidi.Emulation.SetGeolocationCoordinatesOverrideAsync(new() { Contexts = [context] }), Throws.Nothing);
74+
Assert.That(async () => await bidi.Emulation.SetGeolocationPositionErrorOverrideAsync(new() { Contexts = [context] }), Throws.Nothing);
6775
}
6876
}

0 commit comments

Comments
 (0)