Skip to content

Commit c811212

Browse files
committed
SetScreenOrientation
1 parent a470cbc commit c811212

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,6 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
177177
[JsonSerializable(typeof(Emulation.SetLocaleOverrideCommand))]
178178
[JsonSerializable(typeof(Emulation.SetForcedColorsModeThemeOverrideCommand))]
179179
[JsonSerializable(typeof(Emulation.SetScriptingEnabledCommand))]
180+
[JsonSerializable(typeof(Emulation.SetScreenOrientationOverrideCommand))]
180181

181182
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+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,11 @@ public async Task<EmptyResult> SetScriptingEnabledAsync(bool? enabled, SetScript
5858

5959
return await Broker.ExecuteCommandAsync<SetScriptingEnabledCommand, EmptyResult>(new SetScriptingEnabledCommand(@params), options).ConfigureAwait(false);
6060
}
61+
62+
public async Task<EmptyResult> SetScreenOrientationOverrideAsync(ScreenOrientation? screenOrientation, SetScreenOrientationOverrideOptions? options = null)
63+
{
64+
var @params = new SetScreenOrientationOverrideParameters(screenOrientation, options?.Contexts, options?.UserContexts);
65+
66+
return await Broker.ExecuteCommandAsync<SetScreenOrientationOverrideCommand, EmptyResult>(new SetScreenOrientationOverrideCommand(@params), options).ConfigureAwait(false);
67+
}
6168
}
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);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,12 @@ public void CanSetScriptingEnabled()
5757
Assert.That(async () => await bidi.Emulation.SetScriptingEnabledAsync(false, new () { Contexts = [context] }), Throws.Nothing);
5858
Assert.That(async () => await bidi.Emulation.SetScriptingEnabledAsync(null, new () { Contexts = [context] }), Throws.Nothing);
5959
}
60+
61+
[Test]
62+
public void CanSetScreenOrientationOverride()
63+
{
64+
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);
67+
}
6068
}

0 commit comments

Comments
 (0)