|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.bidi.emulation; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.openqa.selenium.JavascriptExecutor; |
| 26 | +import org.openqa.selenium.WindowType; |
| 27 | +import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; |
| 28 | +import org.openqa.selenium.bidi.browsingcontext.CreateContextParameters; |
| 29 | +import org.openqa.selenium.bidi.browsingcontext.ReadinessState; |
| 30 | +import org.openqa.selenium.bidi.module.Browser; |
| 31 | +import org.openqa.selenium.testing.JupiterTestBase; |
| 32 | +import org.openqa.selenium.testing.NeedsFreshDriver; |
| 33 | + |
| 34 | +public class SetScreenOrientationOverrideTest extends JupiterTestBase { |
| 35 | + |
| 36 | + private Map<String, Object> getScreenOrientation(String context) { |
| 37 | + driver.switchTo().window(context); |
| 38 | + JavascriptExecutor executor = (JavascriptExecutor) driver; |
| 39 | + |
| 40 | + Map<String, Object> orientation = |
| 41 | + (Map<String, Object>) |
| 42 | + executor.executeScript( |
| 43 | + "return { type: screen.orientation.type, angle: screen.orientation.angle };"); |
| 44 | + |
| 45 | + return Map.of( |
| 46 | + "type", orientation.get("type"), "angle", ((Number) orientation.get("angle")).intValue()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @NeedsFreshDriver |
| 51 | + void canSetScreenOrientationOverrideInContext() { |
| 52 | + BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); |
| 53 | + String contextId = context.getId(); |
| 54 | + |
| 55 | + // Navigate to a page first to ensure screen.orientation is available |
| 56 | + String url = appServer.whereIs("formPage.html"); |
| 57 | + context.navigate(url, ReadinessState.COMPLETE); |
| 58 | + |
| 59 | + Map<String, Object> initialOrientation = getScreenOrientation(contextId); |
| 60 | + |
| 61 | + Emulation emulation = new Emulation(driver); |
| 62 | + |
| 63 | + // Set landscape-primary orientation |
| 64 | + ScreenOrientation landscapeOrientation = |
| 65 | + new ScreenOrientation( |
| 66 | + ScreenOrientationNatural.LANDSCAPE, ScreenOrientationType.LANDSCAPE_PRIMARY); |
| 67 | + emulation.setScreenOrientationOverride( |
| 68 | + new SetScreenOrientationOverrideParameters(landscapeOrientation) |
| 69 | + .contexts(List.of(contextId))); |
| 70 | + |
| 71 | + Map<String, Object> currentOrientation = getScreenOrientation(contextId); |
| 72 | + assertThat(currentOrientation.get("type")).isEqualTo("landscape-primary"); |
| 73 | + assertThat(currentOrientation.get("angle")).isEqualTo(0); |
| 74 | + |
| 75 | + // Set portrait-secondary orientation |
| 76 | + ScreenOrientation portraitOrientation = |
| 77 | + new ScreenOrientation( |
| 78 | + ScreenOrientationNatural.PORTRAIT, ScreenOrientationType.PORTRAIT_SECONDARY); |
| 79 | + emulation.setScreenOrientationOverride( |
| 80 | + new SetScreenOrientationOverrideParameters(portraitOrientation) |
| 81 | + .contexts(List.of(contextId))); |
| 82 | + |
| 83 | + currentOrientation = getScreenOrientation(contextId); |
| 84 | + assertThat(currentOrientation.get("type")).isEqualTo("portrait-secondary"); |
| 85 | + assertThat(currentOrientation.get("angle")).isEqualTo(180); |
| 86 | + |
| 87 | + // Clear the override |
| 88 | + emulation.setScreenOrientationOverride( |
| 89 | + new SetScreenOrientationOverrideParameters(null).contexts(List.of(contextId))); |
| 90 | + |
| 91 | + currentOrientation = getScreenOrientation(contextId); |
| 92 | + assertThat(currentOrientation.get("type")).isEqualTo(initialOrientation.get("type")); |
| 93 | + assertThat(currentOrientation.get("angle")).isEqualTo(initialOrientation.get("angle")); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + @NeedsFreshDriver |
| 98 | + void canSetScreenOrientationOverrideInUserContext() { |
| 99 | + Browser browser = new Browser(driver); |
| 100 | + String userContext = browser.createUserContext(); |
| 101 | + |
| 102 | + try { |
| 103 | + BrowsingContext context = |
| 104 | + new BrowsingContext( |
| 105 | + driver, new CreateContextParameters(WindowType.TAB).userContext(userContext)); |
| 106 | + String contextId = context.getId(); |
| 107 | + |
| 108 | + try { |
| 109 | + driver.switchTo().window(contextId); |
| 110 | + |
| 111 | + Emulation emulation = new Emulation(driver); |
| 112 | + |
| 113 | + // Navigate to a page first to ensure screen.orientation is available |
| 114 | + String url = appServer.whereIs("formPage.html"); |
| 115 | + context.navigate(url, ReadinessState.COMPLETE); |
| 116 | + |
| 117 | + Map<String, Object> initialOrientation = getScreenOrientation(contextId); |
| 118 | + |
| 119 | + // Set landscape-primary orientation |
| 120 | + ScreenOrientation landscapeOrientation = |
| 121 | + new ScreenOrientation( |
| 122 | + ScreenOrientationNatural.LANDSCAPE, ScreenOrientationType.LANDSCAPE_PRIMARY); |
| 123 | + emulation.setScreenOrientationOverride( |
| 124 | + new SetScreenOrientationOverrideParameters(landscapeOrientation) |
| 125 | + .userContexts(List.of(userContext))); |
| 126 | + |
| 127 | + Map<String, Object> currentOrientation = getScreenOrientation(contextId); |
| 128 | + assertThat(currentOrientation.get("type")).isEqualTo("landscape-primary"); |
| 129 | + assertThat(currentOrientation.get("angle")).isEqualTo(0); |
| 130 | + |
| 131 | + // Set portrait-secondary orientation |
| 132 | + ScreenOrientation portraitOrientation = |
| 133 | + new ScreenOrientation( |
| 134 | + ScreenOrientationNatural.PORTRAIT, ScreenOrientationType.PORTRAIT_SECONDARY); |
| 135 | + emulation.setScreenOrientationOverride( |
| 136 | + new SetScreenOrientationOverrideParameters(portraitOrientation) |
| 137 | + .userContexts(List.of(userContext))); |
| 138 | + |
| 139 | + currentOrientation = getScreenOrientation(contextId); |
| 140 | + assertThat(currentOrientation.get("type")).isEqualTo("portrait-secondary"); |
| 141 | + assertThat(currentOrientation.get("angle")).isEqualTo(180); |
| 142 | + |
| 143 | + // Clear the override |
| 144 | + emulation.setScreenOrientationOverride( |
| 145 | + new SetScreenOrientationOverrideParameters(null).userContexts(List.of(userContext))); |
| 146 | + |
| 147 | + currentOrientation = getScreenOrientation(contextId); |
| 148 | + assertThat(currentOrientation.get("type")).isEqualTo(initialOrientation.get("type")); |
| 149 | + assertThat(currentOrientation.get("angle")).isEqualTo(initialOrientation.get("angle")); |
| 150 | + |
| 151 | + } finally { |
| 152 | + context.close(); |
| 153 | + } |
| 154 | + } finally { |
| 155 | + browser.removeUserContext(userContext); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments