Skip to content

Commit 8749a53

Browse files
authored
[java][BiDi] implement emulation.setScreenOrientationOverride (#16705)
1 parent 88a8296 commit 8749a53

File tree

6 files changed

+321
-0
lines changed

6 files changed

+321
-0
lines changed

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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.Map;
21+
import org.openqa.selenium.internal.Require;
22+
23+
public class ScreenOrientation {
24+
private final ScreenOrientationNatural natural;
25+
private final ScreenOrientationType type;
26+
27+
public ScreenOrientation(ScreenOrientationNatural natural, ScreenOrientationType type) {
28+
this.natural = Require.nonNull("natural", natural);
29+
this.type = Require.nonNull("type", type);
30+
}
31+
32+
public ScreenOrientationNatural getNatural() {
33+
return natural;
34+
}
35+
36+
public ScreenOrientationType getType() {
37+
return type;
38+
}
39+
40+
public Map<String, Object> toMap() {
41+
return Map.of(
42+
"natural", natural.toString(),
43+
"type", type.toString());
44+
}
45+
}
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: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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

Comments
 (0)