From b943316c8e6dab7589cb9f8c977f9881c2f9a8d9 Mon Sep 17 00:00:00 2001 From: Delta456 Date: Tue, 9 Dec 2025 00:06:41 +0530 Subject: [PATCH 1/2] [java][BiDi] implement `emulation.setScreenOrientationOverride` --- .../selenium/bidi/emulation/Emulation.java | 7 + .../bidi/emulation/ScreenOrientation.java | 52 ++++++ .../emulation/ScreenOrientationNatural.java | 34 ++++ .../bidi/emulation/ScreenOrientationType.java | 36 ++++ ...etScreenOrientationOverrideParameters.java | 41 +++++ .../SetScreenOrientationOverrideTest.java | 161 ++++++++++++++++++ 6 files changed, 331 insertions(+) create mode 100644 java/src/org/openqa/selenium/bidi/emulation/ScreenOrientation.java create mode 100644 java/src/org/openqa/selenium/bidi/emulation/ScreenOrientationNatural.java create mode 100644 java/src/org/openqa/selenium/bidi/emulation/ScreenOrientationType.java create mode 100644 java/src/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideParameters.java create mode 100644 java/test/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideTest.java diff --git a/java/src/org/openqa/selenium/bidi/emulation/Emulation.java b/java/src/org/openqa/selenium/bidi/emulation/Emulation.java index 1e885559cdff6..9b99568ac016b 100644 --- a/java/src/org/openqa/selenium/bidi/emulation/Emulation.java +++ b/java/src/org/openqa/selenium/bidi/emulation/Emulation.java @@ -60,4 +60,11 @@ public void setUserAgentOverride(SetUserAgentOverrideParameters parameters) { bidi.send(new Command<>("emulation.setUserAgentOverride", parameters.toMap(), Map.class)); } + + public void setScreenOrientationOverride(SetScreenOrientationOverrideParameters parameters) { + Require.nonNull("SetScreenOrientationOverride parameters", parameters); + + bidi.send( + new Command<>("emulation.setScreenOrientationOverride", parameters.toMap(), Map.class)); + } } diff --git a/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientation.java b/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientation.java new file mode 100644 index 0000000000000..ecdbb1b27cdd6 --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientation.java @@ -0,0 +1,52 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.openqa.selenium.bidi.emulation; + +import java.util.HashMap; +import java.util.Map; + +public class ScreenOrientation { + private final ScreenOrientationNatural natural; + private final ScreenOrientationType type; + + public ScreenOrientation(ScreenOrientationNatural natural, ScreenOrientationType type) { + if (natural == null) { + throw new IllegalArgumentException("Natural orientation cannot be null"); + } + if (type == null) { + throw new IllegalArgumentException("Orientation type cannot be null"); + } + this.natural = natural; + this.type = type; + } + + public ScreenOrientationNatural getNatural() { + return natural; + } + + public ScreenOrientationType getType() { + return type; + } + + public Map toMap() { + Map map = new HashMap<>(); + map.put("natural", natural.toString()); + map.put("type", type.toString()); + return map; + } +} diff --git a/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientationNatural.java b/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientationNatural.java new file mode 100644 index 0000000000000..72d7f8b2c6d8f --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientationNatural.java @@ -0,0 +1,34 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.openqa.selenium.bidi.emulation; + +public enum ScreenOrientationNatural { + PORTRAIT("portrait"), + LANDSCAPE("landscape"); + + private final String value; + + ScreenOrientationNatural(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } +} diff --git a/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientationType.java b/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientationType.java new file mode 100644 index 0000000000000..a7c01e5203203 --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientationType.java @@ -0,0 +1,36 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.openqa.selenium.bidi.emulation; + +public enum ScreenOrientationType { + PORTRAIT_PRIMARY("portrait-primary"), + PORTRAIT_SECONDARY("portrait-secondary"), + LANDSCAPE_PRIMARY("landscape-primary"), + LANDSCAPE_SECONDARY("landscape-secondary"); + + private final String value; + + ScreenOrientationType(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } +} diff --git a/java/src/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideParameters.java b/java/src/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideParameters.java new file mode 100644 index 0000000000000..6c63871d2d79d --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideParameters.java @@ -0,0 +1,41 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.openqa.selenium.bidi.emulation; + +public class SetScreenOrientationOverrideParameters extends AbstractOverrideParameters { + + public SetScreenOrientationOverrideParameters(ScreenOrientation screenOrientation) { + if (screenOrientation == null) { + map.put("screenOrientation", null); + } else { + map.put("screenOrientation", screenOrientation.toMap()); + } + } + + @Override + public SetScreenOrientationOverrideParameters contexts(java.util.List contexts) { + super.contexts(contexts); + return this; + } + + @Override + public SetScreenOrientationOverrideParameters userContexts(java.util.List userContexts) { + super.userContexts(userContexts); + return this; + } +} diff --git a/java/test/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideTest.java b/java/test/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideTest.java new file mode 100644 index 0000000000000..4575cfa7d2963 --- /dev/null +++ b/java/test/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideTest.java @@ -0,0 +1,161 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.openqa.selenium.bidi.emulation; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WindowType; +import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; +import org.openqa.selenium.bidi.browsingcontext.CreateContextParameters; +import org.openqa.selenium.bidi.browsingcontext.ReadinessState; +import org.openqa.selenium.bidi.module.Browser; +import org.openqa.selenium.testing.JupiterTestBase; +import org.openqa.selenium.testing.NeedsFreshDriver; + +public class SetScreenOrientationOverrideTest extends JupiterTestBase { + + private Map getScreenOrientation(String context) { + driver.switchTo().window(context); + JavascriptExecutor executor = (JavascriptExecutor) driver; + + String type = (String) executor.executeScript("return screen.orientation.type;"); + Number angle = (Number) executor.executeScript("return screen.orientation.angle;"); + + return Map.of("type", type, "angle", angle.intValue()); + } + + @Test + @NeedsFreshDriver + void canSetScreenOrientationOverrideInContext() { + BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); + String contextId = context.getId(); + + // Navigate to a page first to ensure screen.orientation is available + String url = appServer.whereIs("formPage.html"); + context.navigate(url, ReadinessState.COMPLETE); + + Map initialOrientation = getScreenOrientation(contextId); + + Emulation emulation = new Emulation(driver); + + // Set landscape-primary orientation + ScreenOrientation landscapeOrientation = + new ScreenOrientation( + ScreenOrientationNatural.LANDSCAPE, ScreenOrientationType.LANDSCAPE_PRIMARY); + emulation.setScreenOrientationOverride( + new SetScreenOrientationOverrideParameters(landscapeOrientation) + .contexts(List.of(contextId))); + + // Reload the page to apply the orientation change + context.navigate(url, ReadinessState.COMPLETE); + + Map currentOrientation = getScreenOrientation(contextId); + assertThat(currentOrientation.get("type")).isEqualTo("landscape-primary"); + assertThat(currentOrientation.get("angle")).isEqualTo(0); + + // Set portrait-secondary orientation + ScreenOrientation portraitOrientation = + new ScreenOrientation( + ScreenOrientationNatural.PORTRAIT, ScreenOrientationType.PORTRAIT_SECONDARY); + emulation.setScreenOrientationOverride( + new SetScreenOrientationOverrideParameters(portraitOrientation) + .contexts(List.of(contextId))); + + currentOrientation = getScreenOrientation(contextId); + assertThat(currentOrientation.get("type")).isEqualTo("portrait-secondary"); + assertThat(currentOrientation.get("angle")).isEqualTo(180); + + // Clear the override + emulation.setScreenOrientationOverride( + new SetScreenOrientationOverrideParameters(null).contexts(List.of(contextId))); + + currentOrientation = getScreenOrientation(contextId); + assertThat(currentOrientation.get("type")).isEqualTo(initialOrientation.get("type")); + assertThat(currentOrientation.get("angle")).isEqualTo(initialOrientation.get("angle")); + } + + @Test + @NeedsFreshDriver + void canSetScreenOrientationOverrideInUserContext() { + Browser browser = new Browser(driver); + String userContext = browser.createUserContext(); + + try { + BrowsingContext context = + new BrowsingContext( + driver, new CreateContextParameters(WindowType.TAB).userContext(userContext)); + String contextId = context.getId(); + + try { + driver.switchTo().window(contextId); + + Emulation emulation = new Emulation(driver); + + // Navigate to a page first to ensure screen.orientation is available + String url = appServer.whereIs("formPage.html"); + context.navigate(url, ReadinessState.COMPLETE); + + Map initialOrientation = getScreenOrientation(contextId); + + // Set landscape-primary orientation + ScreenOrientation landscapeOrientation = + new ScreenOrientation( + ScreenOrientationNatural.LANDSCAPE, ScreenOrientationType.LANDSCAPE_PRIMARY); + emulation.setScreenOrientationOverride( + new SetScreenOrientationOverrideParameters(landscapeOrientation) + .userContexts(List.of(userContext))); + + // Reload the page to apply the orientation override + context.navigate(url, ReadinessState.COMPLETE); + + Map currentOrientation = getScreenOrientation(contextId); + assertThat(currentOrientation.get("type")).isEqualTo("landscape-primary"); + assertThat(currentOrientation.get("angle")).isEqualTo(0); + + // Set portrait-secondary orientation + ScreenOrientation portraitOrientation = + new ScreenOrientation( + ScreenOrientationNatural.PORTRAIT, ScreenOrientationType.PORTRAIT_SECONDARY); + emulation.setScreenOrientationOverride( + new SetScreenOrientationOverrideParameters(portraitOrientation) + .userContexts(List.of(userContext))); + + currentOrientation = getScreenOrientation(contextId); + assertThat(currentOrientation.get("type")).isEqualTo("portrait-secondary"); + assertThat(currentOrientation.get("angle")).isEqualTo(180); + + // Clear the override + emulation.setScreenOrientationOverride( + new SetScreenOrientationOverrideParameters(null).userContexts(List.of(userContext))); + + currentOrientation = getScreenOrientation(contextId); + assertThat(currentOrientation.get("type")).isEqualTo(initialOrientation.get("type")); + assertThat(currentOrientation.get("angle")).isEqualTo(initialOrientation.get("angle")); + + } finally { + context.close(); + } + } finally { + browser.removeUserContext(userContext); + } + } +} From 56806888e2a05d02a82a4ff4781fe8ed1e8118b8 Mon Sep 17 00:00:00 2001 From: Delta456 Date: Wed, 10 Dec 2025 00:30:04 +0530 Subject: [PATCH 2/2] refactor --- .../bidi/emulation/ScreenOrientation.java | 19 ++++++------------- .../SetScreenOrientationOverrideTest.java | 15 ++++++--------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientation.java b/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientation.java index ecdbb1b27cdd6..8bb9959234767 100644 --- a/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientation.java +++ b/java/src/org/openqa/selenium/bidi/emulation/ScreenOrientation.java @@ -17,22 +17,16 @@ package org.openqa.selenium.bidi.emulation; -import java.util.HashMap; import java.util.Map; +import org.openqa.selenium.internal.Require; public class ScreenOrientation { private final ScreenOrientationNatural natural; private final ScreenOrientationType type; public ScreenOrientation(ScreenOrientationNatural natural, ScreenOrientationType type) { - if (natural == null) { - throw new IllegalArgumentException("Natural orientation cannot be null"); - } - if (type == null) { - throw new IllegalArgumentException("Orientation type cannot be null"); - } - this.natural = natural; - this.type = type; + this.natural = Require.nonNull("natural", natural); + this.type = Require.nonNull("type", type); } public ScreenOrientationNatural getNatural() { @@ -44,9 +38,8 @@ public ScreenOrientationType getType() { } public Map toMap() { - Map map = new HashMap<>(); - map.put("natural", natural.toString()); - map.put("type", type.toString()); - return map; + return Map.of( + "natural", natural.toString(), + "type", type.toString()); } } diff --git a/java/test/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideTest.java b/java/test/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideTest.java index 4575cfa7d2963..bcb35559b7d7b 100644 --- a/java/test/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideTest.java +++ b/java/test/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideTest.java @@ -37,10 +37,13 @@ private Map getScreenOrientation(String context) { driver.switchTo().window(context); JavascriptExecutor executor = (JavascriptExecutor) driver; - String type = (String) executor.executeScript("return screen.orientation.type;"); - Number angle = (Number) executor.executeScript("return screen.orientation.angle;"); + Map orientation = + (Map) + executor.executeScript( + "return { type: screen.orientation.type, angle: screen.orientation.angle };"); - return Map.of("type", type, "angle", angle.intValue()); + return Map.of( + "type", orientation.get("type"), "angle", ((Number) orientation.get("angle")).intValue()); } @Test @@ -65,9 +68,6 @@ void canSetScreenOrientationOverrideInContext() { new SetScreenOrientationOverrideParameters(landscapeOrientation) .contexts(List.of(contextId))); - // Reload the page to apply the orientation change - context.navigate(url, ReadinessState.COMPLETE); - Map currentOrientation = getScreenOrientation(contextId); assertThat(currentOrientation.get("type")).isEqualTo("landscape-primary"); assertThat(currentOrientation.get("angle")).isEqualTo(0); @@ -124,9 +124,6 @@ void canSetScreenOrientationOverrideInUserContext() { new SetScreenOrientationOverrideParameters(landscapeOrientation) .userContexts(List.of(userContext))); - // Reload the page to apply the orientation override - context.navigate(url, ReadinessState.COMPLETE); - Map currentOrientation = getScreenOrientation(contextId); assertThat(currentOrientation.get("type")).isEqualTo("landscape-primary"); assertThat(currentOrientation.get("angle")).isEqualTo(0);