Skip to content

Commit 380b2c6

Browse files
rafe-glukeis
authored andcommitted
DeviceRotation implementation (#2482)
1 parent 2b06394 commit 380b2c6

File tree

6 files changed

+144
-8
lines changed

6 files changed

+144
-8
lines changed

java/client/src/org/openqa/selenium/BUCK

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ java_library(name = 'core',
2828
'ContextAware.java',
2929
'HasCapabilities.java',
3030
'JavascriptExecutor.java',
31+
'DeviceRotation.java',
3132
'Keys.java',
3233
'OutputType.java',
3334
'Proxy.java',
@@ -59,6 +60,7 @@ java_library(name = 'core',
5960
'//java/client/src/org/openqa/selenium/interactions:exceptions',
6061
'//java/client/src/org/openqa/selenium/logging:api',
6162
'//java/client/src/org/openqa/selenium/security:security',
63+
'//third_party/java/guava:guava',
6264
],
6365
visibility = [
6466
'//java/client/src/org/openqa/selenium/interactions:interactions',
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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;
19+
20+
import java.util.Map;
21+
import com.google.common.collect.ImmutableMap;
22+
23+
/**
24+
* Defines an object which represents the three dimensional plane and how a device can be rotated about it.
25+
* Each of the axis is in positive degrees on the real number scale (0 <= deg <= 360).
26+
*
27+
* Example Instantiation to rotate device to "Landscape Right":
28+
* DeviceRotation(0, 0, 90);
29+
*/
30+
public class DeviceRotation {
31+
//Default orientation is portrait
32+
private int x = 0;
33+
private int y = 0;
34+
private int z = 0;
35+
36+
/**
37+
* Instantiate a DeviceRotation object based on three integers.
38+
* @param x
39+
* @param y
40+
* @param z
41+
*/
42+
public DeviceRotation(int x, int y, int z) {
43+
this.validateParameters(x, y, z);
44+
this.x = x;
45+
this.y = y;
46+
this.z = z;
47+
}
48+
49+
/**
50+
* Instantiate a DeviceRotation object based on a
51+
* HashMap object where the keys are the axis x, y, and z respectively:
52+
* x : xVal
53+
* y : yVal
54+
* z : zVal
55+
* @param map
56+
*/
57+
public DeviceRotation(Map<String, Integer> map) {
58+
if (map == null || !map.containsKey("x") || !map.containsKey("y") || !map.containsKey("z")) {
59+
throw new IllegalArgumentException("Could not initialize DeviceRotation with map given: " + map.toString());
60+
}
61+
this.validateParameters(map.get("x"), map.get("y"), map.get("z"));
62+
this.x = map.get("x");
63+
this.y = map.get("y");
64+
this.z = map.get("z");
65+
}
66+
67+
private void validateParameters(int x, int y, int z) {
68+
if (x < 0 || y < 0 || z < 0) {
69+
throw new IllegalArgumentException("DeviceRotation requires positive axis values: \nx = " + x + "\ny = " + y + "\nz = " + z);
70+
}
71+
}
72+
73+
/**
74+
* @return the x
75+
*/
76+
public int getX() {
77+
return x;
78+
}
79+
80+
/**
81+
* @return the y
82+
*/
83+
public int getY() {
84+
return y;
85+
}
86+
87+
/**
88+
* @return the z
89+
*/
90+
public int getZ() {
91+
return z;
92+
}
93+
94+
/**
95+
* @return returns all axis mapped to an ImmutableMap
96+
*/
97+
public ImmutableMap<String,Integer> parameters() {
98+
return ImmutableMap.of("x", this.x, "y", this.y, "z", this.z);
99+
}
100+
101+
102+
}

java/client/src/org/openqa/selenium/Rotatable.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,17 @@ public interface Rotatable {
3636
* @return the current screen orientation of the browser
3737
*/
3838
ScreenOrientation getOrientation();
39+
40+
/**
41+
* Changes the rotation of the browser window.
42+
*
43+
* @param rotation
44+
*/
45+
void rotate(DeviceRotation rotation);
46+
47+
/**
48+
* @return DeviceOrientation describing the current screen rotation of the browser window
49+
*/
50+
DeviceRotation rotation();
51+
3952
}

java/client/src/org/openqa/selenium/remote/AddRotatable.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,43 @@
1919

2020
import com.google.common.collect.ImmutableMap;
2121

22+
import org.openqa.selenium.DeviceRotation;
2223
import org.openqa.selenium.Rotatable;
2324
import org.openqa.selenium.ScreenOrientation;
2425

2526
import java.lang.reflect.Method;
2627

2728
public class AddRotatable implements AugmenterProvider {
28-
29+
2930
public Class<?> getDescribedInterface() {
3031
return Rotatable.class;
3132
}
3233

3334
public InterfaceImplementation getImplementation(Object value) {
3435
return new InterfaceImplementation() {
3536
public Object invoke(ExecuteMethod executeMethod, Object self, Method method, Object... args) {
36-
if ("rotate".equals(method.getName())) {
37-
return executeMethod.execute(DriverCommand.SET_SCREEN_ORIENTATION,
38-
ImmutableMap.of("orientation", args[0]));
39-
} else if ("getOrientation".equals(method.getName())) {
40-
return ScreenOrientation.valueOf((String) executeMethod.execute(
41-
DriverCommand.GET_SCREEN_ORIENTATION, null));
37+
String m = method.getName();
38+
Object response;
39+
switch(m) {
40+
case "rotate":
41+
if (args[0] instanceof ScreenOrientation) {
42+
response = executeMethod.execute(DriverCommand.SET_SCREEN_ORIENTATION, ImmutableMap.of("orientation", args[0]));
43+
} else if (args[0] instanceof DeviceRotation) {
44+
response = executeMethod.execute(DriverCommand.SET_SCREEN_ORIENTATION, ((DeviceRotation)args[0]).parameters());
45+
} else {
46+
throw new IllegalArgumentException("rotate parameter must be either of type 'ScreenOrientation' or 'DeviceRotation'");
47+
}
48+
break;
49+
case "getOrientation":
50+
response = ScreenOrientation.valueOf((String) executeMethod.execute(DriverCommand.GET_SCREEN_ORIENTATION, null));
51+
break;
52+
case "rotation":
53+
response = (DeviceRotation) executeMethod.execute(DriverCommand.GET_SCREEN_ROTATION, null);
54+
break;
55+
default:
56+
throw new IllegalArgumentException(method.getName() + ", Not defined in rotatable interface");
4257
}
43-
return null;
58+
return response;
4459
}
4560
};
4661
}

java/client/src/org/openqa/selenium/remote/DriverCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ public interface DriverCommand {
134134

135135
String SET_SCREEN_ORIENTATION = "setScreenOrientation";
136136
String GET_SCREEN_ORIENTATION = "getScreenOrientation";
137+
String SET_SCREEN_ROTATION = "setScreenRotation";
138+
String GET_SCREEN_ROTATION = "getScreenRotation";
137139

138140
String ACTION_CHAIN = "actionChain";
139141

java/client/src/org/openqa/selenium/remote/http/JsonHttpCommandCodec.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ public JsonHttpCommandCodec() {
183183

184184
defineCommand(GET_SCREEN_ORIENTATION, get("/session/:sessionId/orientation"));
185185
defineCommand(SET_SCREEN_ORIENTATION, post("/session/:sessionId/orientation"));
186+
defineCommand(GET_SCREEN_ROTATION, get("/session/:sessionId/rotation"));
187+
defineCommand(SET_SCREEN_ROTATION, post("/session/:sessionId/rotation"));
186188

187189
// Interactions-related commands.
188190
defineCommand(MOUSE_DOWN, post("/session/:sessionId/buttondown"));

0 commit comments

Comments
 (0)