-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
[java][BiDi] implement emulation.setScreenOrientationOverride
#16705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b943316
[java][BiDi] implement `emulation.setScreenOrientationOverride`
Delta456 c719c86
Merge branch 'trunk' into emul_screenorient
Delta456 4f2c9ca
Merge branch 'trunk' into emul_screenorient
Delta456 5680688
refactor
Delta456 7af85f8
Merge branch 'trunk' into emul_screenorient
Delta456 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
java/src/org/openqa/selenium/bidi/emulation/ScreenOrientation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| } | ||
|
Delta456 marked this conversation as resolved.
Outdated
|
||
| this.natural = natural; | ||
| this.type = type; | ||
| } | ||
|
|
||
| public ScreenOrientationNatural getNatural() { | ||
| return natural; | ||
| } | ||
|
|
||
| public ScreenOrientationType getType() { | ||
| return type; | ||
| } | ||
|
|
||
| public Map<String, Object> toMap() { | ||
| Map<String, Object> map = new HashMap<>(); | ||
| map.put("natural", natural.toString()); | ||
| map.put("type", type.toString()); | ||
| return map; | ||
|
Delta456 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
java/src/org/openqa/selenium/bidi/emulation/ScreenOrientationNatural.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
java/src/org/openqa/selenium/bidi/emulation/ScreenOrientationType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
java/src/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideParameters.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String> contexts) { | ||
| super.contexts(contexts); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public SetScreenOrientationOverrideParameters userContexts(java.util.List<String> userContexts) { | ||
| super.userContexts(userContexts); | ||
| return this; | ||
| } | ||
| } |
161 changes: 161 additions & 0 deletions
161
java/test/org/openqa/selenium/bidi/emulation/SetScreenOrientationOverrideTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.