Skip to content

Commit 565459e

Browse files
authored
[dotnet] [bidi] Emulation module (#16380)
1 parent ad3a864 commit 565459e

13 files changed

+668
-0
lines changed

dotnet/src/webdriver/BiDi/BiDi.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public sealed class BiDi : IAsyncDisposable
3737
private Log.LogModule? _logModule;
3838
private Storage.StorageModule? _storageModule;
3939
private WebExtension.WebExtensionModule? _webExtensionModule;
40+
private Emulation.EmulationModule? _emulationModule;
4041

4142
private readonly object _moduleLock = new();
4243

@@ -164,6 +165,19 @@ public WebExtension.WebExtensionModule WebExtension
164165
}
165166
}
166167

168+
public Emulation.EmulationModule Emulation
169+
{
170+
get
171+
{
172+
if (_emulationModule is not null) return _emulationModule;
173+
lock (_moduleLock)
174+
{
175+
_emulationModule ??= new Emulation.EmulationModule(_broker);
176+
}
177+
return _emulationModule;
178+
}
179+
}
180+
167181
public Task<Session.StatusResult> StatusAsync()
168182
{
169183
return SessionModule.StatusAsync();

dotnet/src/webdriver/BiDi/Communication/Broker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ internal Broker(BiDi bidi, Uri url)
8484
new PreloadScriptConverter(_bidi),
8585
new RealmConverter(_bidi),
8686
new RealmTypeConverter(),
87+
new ScreenOrientationTypeConverter(),
8788
new DateTimeOffsetConverter(),
8889
new PrintPageRangeConverter(),
8990
new InputOriginConverter(),

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,12 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
172172
[JsonSerializable(typeof(WebExtension.InstallCommand))]
173173
[JsonSerializable(typeof(WebExtension.InstallResult))]
174174
[JsonSerializable(typeof(WebExtension.UninstallCommand))]
175+
[JsonSerializable(typeof(Emulation.SetTimezoneOverrideCommand))]
176+
[JsonSerializable(typeof(Emulation.SetUserAgentOverrideCommand))]
177+
[JsonSerializable(typeof(Emulation.SetLocaleOverrideCommand))]
178+
[JsonSerializable(typeof(Emulation.SetForcedColorsModeThemeOverrideCommand))]
179+
[JsonSerializable(typeof(Emulation.SetScriptingEnabledCommand))]
180+
[JsonSerializable(typeof(Emulation.SetScreenOrientationOverrideCommand))]
181+
[JsonSerializable(typeof(Emulation.SetGeolocationOverrideCommand))]
175182

176183
internal partial class BiDiJsonSerializerContext : JsonSerializerContext;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// <copyright file="ScreenOrientationTypeConverter.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 to 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;
21+
using System.Text.Json;
22+
using System.Text.Json.Serialization;
23+
using OpenQA.Selenium.BiDi.Emulation;
24+
25+
namespace OpenQA.Selenium.BiDi.Communication.Json.Converters;
26+
27+
internal class ScreenOrientationTypeConverter : JsonConverter<ScreenOrientationType>
28+
{
29+
public override ScreenOrientationType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
30+
{
31+
var str = reader.GetString();
32+
return str switch
33+
{
34+
"portrait-primary" => ScreenOrientationType.PortraitPrimary,
35+
"portrait-secondary" => ScreenOrientationType.PortraitSecondary,
36+
"landscape-primary" => ScreenOrientationType.LandscapePrimary,
37+
"landscape-secondary" => ScreenOrientationType.LandscapeSecondary,
38+
_ => throw new JsonException($"Unrecognized '{str}' value of {typeof(ScreenOrientationType)}."),
39+
};
40+
}
41+
42+
public override void Write(Utf8JsonWriter writer, ScreenOrientationType value, JsonSerializerOptions options)
43+
{
44+
var str = value switch
45+
{
46+
ScreenOrientationType.PortraitPrimary => "portrait-primary",
47+
ScreenOrientationType.PortraitSecondary => "portrait-secondary",
48+
ScreenOrientationType.LandscapePrimary => "landscape-primary",
49+
ScreenOrientationType.LandscapeSecondary => "landscape-secondary",
50+
_ => throw new JsonException($"Unrecognized '{value}' value of {typeof(ScreenOrientationType)}."),
51+
};
52+
53+
writer.WriteStringValue(str);
54+
}
55+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// <copyright file="EmulationModule.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 to 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;
21+
using System.Threading.Tasks;
22+
using OpenQA.Selenium.BiDi.Communication;
23+
24+
namespace OpenQA.Selenium.BiDi.Emulation;
25+
26+
public sealed class EmulationModule(Broker broker) : Module(broker)
27+
{
28+
public async Task<EmptyResult> SetTimezoneOverrideAsync(string? timezone, SetTimezoneOverrideOptions? options = null)
29+
{
30+
var @params = new SetTimezoneOverrideParameters(timezone, options?.Contexts, options?.UserContexts);
31+
32+
return await Broker.ExecuteCommandAsync<SetTimezoneOverrideCommand, EmptyResult>(new SetTimezoneOverrideCommand(@params), options).ConfigureAwait(false);
33+
}
34+
35+
public async Task<EmptyResult> SetUserAgentOverrideAsync(string? userAgent, SetUserAgentOverrideOptions? options = null)
36+
{
37+
var @params = new SetUserAgentOverrideParameters(userAgent, options?.Contexts, options?.UserContexts);
38+
39+
return await Broker.ExecuteCommandAsync<SetUserAgentOverrideCommand, EmptyResult>(new SetUserAgentOverrideCommand(@params), options).ConfigureAwait(false);
40+
}
41+
42+
public async Task<EmptyResult> SetLocaleOverrideAsync(string? locale, SetLocaleOverrideOptions? options = null)
43+
{
44+
var @params = new SetLocaleOverrideParameters(locale, options?.Contexts, options?.UserContexts);
45+
46+
return await Broker.ExecuteCommandAsync<SetLocaleOverrideCommand, EmptyResult>(new SetLocaleOverrideCommand(@params), options).ConfigureAwait(false);
47+
}
48+
49+
public async Task<EmptyResult> SetForcedColorsModeThemeOverrideAsync(ForcedColorsModeTheme? theme, SetForcedColorsModeThemeOverrideOptions? options = null)
50+
{
51+
var @params = new SetForcedColorsModeThemeOverrideParameters(theme, options?.Contexts, options?.UserContexts);
52+
53+
return await Broker.ExecuteCommandAsync<SetForcedColorsModeThemeOverrideCommand, EmptyResult>(new SetForcedColorsModeThemeOverrideCommand(@params), options).ConfigureAwait(false);
54+
}
55+
56+
public async Task<EmptyResult> SetScriptingEnabledAsync(bool? enabled, SetScriptingEnabledOptions? options = null)
57+
{
58+
var @params = new SetScriptingEnabledParameters(enabled, options?.Contexts, options?.UserContexts);
59+
60+
return await Broker.ExecuteCommandAsync<SetScriptingEnabledCommand, EmptyResult>(new SetScriptingEnabledCommand(@params), options).ConfigureAwait(false);
61+
}
62+
63+
public async Task<EmptyResult> SetScreenOrientationOverrideAsync(ScreenOrientation? screenOrientation, SetScreenOrientationOverrideOptions? options = null)
64+
{
65+
var @params = new SetScreenOrientationOverrideParameters(screenOrientation, options?.Contexts, options?.UserContexts);
66+
67+
return await Broker.ExecuteCommandAsync<SetScreenOrientationOverrideCommand, EmptyResult>(new SetScreenOrientationOverrideCommand(@params), options).ConfigureAwait(false);
68+
}
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+
}
92+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// <copyright file="SetForcedColorsModeThemeOverrideCommand.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 to 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 SetForcedColorsModeThemeOverrideCommand(SetForcedColorsModeThemeOverrideParameters @params)
27+
: Command<SetForcedColorsModeThemeOverrideParameters, EmptyResult>(@params, "emulation.setForcedColorsModeThemeOverride");
28+
29+
internal sealed record SetForcedColorsModeThemeOverrideParameters([property: JsonIgnore(Condition = JsonIgnoreCondition.Never)] ForcedColorsModeTheme? Theme, IEnumerable<BrowsingContext.BrowsingContext>? Contexts, IEnumerable<Browser.UserContext>? UserContexts) : Parameters;
30+
31+
public sealed class SetForcedColorsModeThemeOverrideOptions : CommandOptions
32+
{
33+
public IEnumerable<BrowsingContext.BrowsingContext>? Contexts { get; set; }
34+
35+
public IEnumerable<Browser.UserContext>? UserContexts { get; set; }
36+
}
37+
38+
public enum ForcedColorsModeTheme
39+
{
40+
Light,
41+
Dark
42+
}
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 to 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;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// <copyright file="SetLocaleOverrideCommand.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 to 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 SetLocaleOverrideCommand(SetLocaleOverrideParameters @params)
27+
: Command<SetLocaleOverrideParameters, EmptyResult>(@params, "emulation.setLocaleOverride");
28+
29+
internal sealed record SetLocaleOverrideParameters([property: JsonIgnore(Condition = JsonIgnoreCondition.Never)] string? Locale, IEnumerable<BrowsingContext.BrowsingContext>? Contexts, IEnumerable<Browser.UserContext>? UserContexts) : Parameters;
30+
31+
public sealed class SetLocaleOverrideOptions : CommandOptions
32+
{
33+
public IEnumerable<BrowsingContext.BrowsingContext>? Contexts { get; set; }
34+
35+
public IEnumerable<Browser.UserContext>? UserContexts { get; set; }
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// <copyright file="SetScreenOrientationOverrideCommand.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 to 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 SetScreenOrientationOverrideCommand(SetScreenOrientationOverrideParameters @params)
27+
: Command<SetScreenOrientationOverrideParameters, EmptyResult>(@params, "emulation.setScreenOrientationOverride");
28+
29+
internal sealed record SetScreenOrientationOverrideParameters([property: JsonIgnore(Condition = JsonIgnoreCondition.Never)] ScreenOrientation? ScreenOrientation, IEnumerable<BrowsingContext.BrowsingContext>? Contexts, IEnumerable<Browser.UserContext>? UserContexts) : Parameters;
30+
31+
public sealed class SetScreenOrientationOverrideOptions : CommandOptions
32+
{
33+
public IEnumerable<BrowsingContext.BrowsingContext>? Contexts { get; set; }
34+
35+
public IEnumerable<Browser.UserContext>? UserContexts { get; set; }
36+
}
37+
38+
public enum ScreenOrientationNatural
39+
{
40+
Portrait,
41+
Landscape
42+
}
43+
44+
public enum ScreenOrientationType
45+
{
46+
PortraitPrimary,
47+
PortraitSecondary,
48+
LandscapePrimary,
49+
LandscapeSecondary
50+
}
51+
52+
public sealed record ScreenOrientation(ScreenOrientationNatural Natural, ScreenOrientationType Type);

0 commit comments

Comments
 (0)