Skip to content

Commit b943316

Browse files
committed
[java][BiDi] implement emulation.setScreenOrientationOverride
1 parent e453a9f commit b943316

6 files changed

Lines changed: 331 additions & 0 deletions

File tree

java/src/org/openqa/selenium/bidi/emulation/Emulation.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,11 @@ public void setUserAgentOverride(SetUserAgentOverrideParameters parameters) {
6060

6161
bidi.send(new Command<>("emulation.setUserAgentOverride", parameters.toMap(), Map.class));
6262
}
63+
64+
public void setScreenOrientationOverride(SetScreenOrientationOverrideParameters parameters) {
65+
Require.nonNull("SetScreenOrientationOverride parameters", parameters);
66+
67+
bidi.send(
68+
new Command<>("emulation.setScreenOrientationOverride", parameters.toMap(), Map.class));
69+
}
6370
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 java.util.HashMap;
21+
import java.util.Map;
22+
23+
public class ScreenOrientation {
24+
private final ScreenOrientationNatural natural;
25+
private final ScreenOrientationType type;
26+
27+
public ScreenOrientation(ScreenOrientationNatural natural, ScreenOrientationType type) {
28+
if (natural == null) {
29+
throw new IllegalArgumentException("Natural orientation cannot be null");
30+
}
31+
if (type == null) {
32+
throw new IllegalArgumentException("Orientation type cannot be null");
33+
}
34+
this.natural = natural;
35+
this.type = type;
36+
}
37+
38+
public ScreenOrientationNatural getNatural() {
39+
return natural;
40+
}
41+
42+
public ScreenOrientationType getType() {
43+
return type;
44+
}
45+
46+
public Map<String, Object> toMap() {
47+
Map<String, Object> map = new HashMap<>();
48+
map.put("natural", natural.toString());
49+
map.put("type", type.toString());
50+
return map;
51+
}
52+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
public enum ScreenOrientationNatural {
21+
PORTRAIT("portrait"),
22+
LANDSCAPE("landscape");
23+
24+
private final String value;
25+
26+
ScreenOrientationNatural(String value) {
27+
this.value = value;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return value;
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
public enum ScreenOrientationType {
21+
PORTRAIT_PRIMARY("portrait-primary"),
22+
PORTRAIT_SECONDARY("portrait-secondary"),
23+
LANDSCAPE_PRIMARY("landscape-primary"),
24+
LANDSCAPE_SECONDARY("landscape-secondary");
25+
26+
private final String value;
27+
28+
ScreenOrientationType(String value) {
29+
this.value = value;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return value;
35+
}
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
public class SetScreenOrientationOverrideParameters extends AbstractOverrideParameters {
21+
22+
public SetScreenOrientationOverrideParameters(ScreenOrientation screenOrientation) {
23+
if (screenOrientation == null) {
24+
map.put("screenOrientation", null);
25+
} else {
26+
map.put("screenOrientation", screenOrientation.toMap());
27+
}
28+
}
29+
30+
@Override
31+
public SetScreenOrientationOverrideParameters contexts(java.util.List<String> contexts) {
32+
super.contexts(contexts);
33+
return this;
34+
}
35+
36+
@Override
37+
public SetScreenOrientationOverrideParameters userContexts(java.util.List<String> userContexts) {
38+
super.userContexts(userContexts);
39+
return this;
40+
}
41+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
String type = (String) executor.executeScript("return screen.orientation.type;");
41+
Number angle = (Number) executor.executeScript("return screen.orientation.angle;");
42+
43+
return Map.of("type", type, "angle", angle.intValue());
44+
}
45+
46+
@Test
47+
@NeedsFreshDriver
48+
void canSetScreenOrientationOverrideInContext() {
49+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
50+
String contextId = context.getId();
51+
52+
// Navigate to a page first to ensure screen.orientation is available
53+
String url = appServer.whereIs("formPage.html");
54+
context.navigate(url, ReadinessState.COMPLETE);
55+
56+
Map<String, Object> initialOrientation = getScreenOrientation(contextId);
57+
58+
Emulation emulation = new Emulation(driver);
59+
60+
// Set landscape-primary orientation
61+
ScreenOrientation landscapeOrientation =
62+
new ScreenOrientation(
63+
ScreenOrientationNatural.LANDSCAPE, ScreenOrientationType.LANDSCAPE_PRIMARY);
64+
emulation.setScreenOrientationOverride(
65+
new SetScreenOrientationOverrideParameters(landscapeOrientation)
66+
.contexts(List.of(contextId)));
67+
68+
// Reload the page to apply the orientation change
69+
context.navigate(url, ReadinessState.COMPLETE);
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+
// Reload the page to apply the orientation override
128+
context.navigate(url, ReadinessState.COMPLETE);
129+
130+
Map<String, Object> currentOrientation = getScreenOrientation(contextId);
131+
assertThat(currentOrientation.get("type")).isEqualTo("landscape-primary");
132+
assertThat(currentOrientation.get("angle")).isEqualTo(0);
133+
134+
// Set portrait-secondary orientation
135+
ScreenOrientation portraitOrientation =
136+
new ScreenOrientation(
137+
ScreenOrientationNatural.PORTRAIT, ScreenOrientationType.PORTRAIT_SECONDARY);
138+
emulation.setScreenOrientationOverride(
139+
new SetScreenOrientationOverrideParameters(portraitOrientation)
140+
.userContexts(List.of(userContext)));
141+
142+
currentOrientation = getScreenOrientation(contextId);
143+
assertThat(currentOrientation.get("type")).isEqualTo("portrait-secondary");
144+
assertThat(currentOrientation.get("angle")).isEqualTo(180);
145+
146+
// Clear the override
147+
emulation.setScreenOrientationOverride(
148+
new SetScreenOrientationOverrideParameters(null).userContexts(List.of(userContext)));
149+
150+
currentOrientation = getScreenOrientation(contextId);
151+
assertThat(currentOrientation.get("type")).isEqualTo(initialOrientation.get("type"));
152+
assertThat(currentOrientation.get("angle")).isEqualTo(initialOrientation.get("angle"));
153+
154+
} finally {
155+
context.close();
156+
}
157+
} finally {
158+
browser.removeUserContext(userContext);
159+
}
160+
}
161+
}

0 commit comments

Comments
 (0)