Skip to content

Commit

Permalink
DeviceRotation implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rafe-g committed Jul 18, 2016
1 parent 0a4b995 commit af490d9
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 8 deletions.
2 changes: 2 additions & 0 deletions java/client/src/org/openqa/selenium/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ java_library(name = 'core',
'ContextAware.java',
'HasCapabilities.java',
'JavascriptExecutor.java',
'DeviceRotation.java',
'Keys.java',
'OutputType.java',
'Proxy.java',
Expand Down Expand Up @@ -60,6 +61,7 @@ java_library(name = 'core',
'//java/client/src/org/openqa/selenium/interactions:exceptions',
'//java/client/src/org/openqa/selenium/logging:api',
'//java/client/src/org/openqa/selenium/security:security',
'//third_party/java/guava:guava',
],
visibility = [
'//java/client/src/org/openqa/selenium/interactions:interactions',
Expand Down
102 changes: 102 additions & 0 deletions java/client/src/org/openqa/selenium/DeviceRotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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;

import java.util.Map;
import com.google.common.collect.ImmutableMap;

/**
* Defines an object which represents the three dimensional plane and how a device can be rotated about it.
* Each of the axis is in positive degrees on the real number scale (0 <= deg <= 360).
*
* Example Instantiation to rotate device to "Landscape Right":
* DeviceRotation(0, 0, 90);
*/
public class DeviceRotation {
//Default orientation is portrait
private int x = 0;
private int y = 0;
private int z = 0;

/**
* Instantiate a DeviceRotation object based on three integers.
* @param x
* @param y
* @param z
*/
public DeviceRotation(int x, int y, int z) {
this.validateParameters(x, y, z);
this.x = x;
this.y = y;
this.z = z;
}

/**
* Instantiate a DeviceRotation object based on a
* HashMap object where the keys are the axis x, y, and z respectively:
* x : xVal
* y : yVal
* z : zVal
* @param map
*/
public DeviceRotation(Map<String, Integer> map) {
if (map == null || !map.containsKey("x") || !map.containsKey("y") || !map.containsKey("z")) {
throw new IllegalArgumentException("Could not initialize DeviceRotation with map given: " + map.toString());
}
this.validateParameters(map.get("x"), map.get("y"), map.get("z"));
this.x = map.get("x");
this.y = map.get("y");
this.z = map.get("z");
}

private void validateParameters(int x, int y, int z) {
if (x < 0 || y < 0 || z < 0) {
throw new IllegalArgumentException("DeviceRotation requires positive axis values: \nx = " + x + "\ny = " + y + "\nz = " + z);
}
}

/**
* @return the x
*/
public int getX() {
return x;
}

/**
* @return the y
*/
public int getY() {
return y;
}

/**
* @return the z
*/
public int getZ() {
return z;
}

/**
* @return returns all axis mapped to an ImmutableMap
*/
public ImmutableMap<String,Integer> parameters() {
return ImmutableMap.of("x", this.x, "y", this.y, "z", this.z);
}


}
13 changes: 13 additions & 0 deletions java/client/src/org/openqa/selenium/Rotatable.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,17 @@ public interface Rotatable {
* @return the current screen orientation of the browser
*/
ScreenOrientation getOrientation();

/**
* Changes the rotation of the browser window.
*
* @param rotation
*/
void rotate(DeviceRotation rotation);

/**
* @return DeviceOrientation describing the current screen rotation of the browser window
*/
DeviceRotation rotation();

}
31 changes: 23 additions & 8 deletions java/client/src/org/openqa/selenium/remote/AddRotatable.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,43 @@

import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.DeviceRotation;
import org.openqa.selenium.Rotatable;
import org.openqa.selenium.ScreenOrientation;

import java.lang.reflect.Method;

public class AddRotatable implements AugmenterProvider {

public Class<?> getDescribedInterface() {
return Rotatable.class;
}

public InterfaceImplementation getImplementation(Object value) {
return new InterfaceImplementation() {
public Object invoke(ExecuteMethod executeMethod, Object self, Method method, Object... args) {
if ("rotate".equals(method.getName())) {
return executeMethod.execute(DriverCommand.SET_SCREEN_ORIENTATION,
ImmutableMap.of("orientation", args[0]));
} else if ("getOrientation".equals(method.getName())) {
return ScreenOrientation.valueOf((String) executeMethod.execute(
DriverCommand.GET_SCREEN_ORIENTATION, null));
String m = method.getName();
Object response;
switch(m) {
case "rotate":
if (args[0] instanceof ScreenOrientation) {
response = executeMethod.execute(DriverCommand.SET_SCREEN_ORIENTATION, ImmutableMap.of("orientation", args[0]));
} else if (args[0] instanceof DeviceRotation) {
response = executeMethod.execute(DriverCommand.SET_SCREEN_ORIENTATION, ((DeviceRotation)args[0]).parameters());
} else {
throw new IllegalArgumentException("rotate parameter must be either of type 'ScreenOrientation' or 'DeviceRotation'");
}
break;
case "getOrientation":
response = ScreenOrientation.valueOf((String) executeMethod.execute(DriverCommand.GET_SCREEN_ORIENTATION, null));
break;
case "rotation":
response = (DeviceRotation) executeMethod.execute(DriverCommand.GET_SCREEN_ROTATION, null);
break;
default:
throw new IllegalArgumentException(method.getName() + ", Not defined in rotatable interface");
}
return null;
return response;
}
};
}
Expand Down
2 changes: 2 additions & 0 deletions java/client/src/org/openqa/selenium/remote/DriverCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public interface DriverCommand {

String SET_SCREEN_ORIENTATION = "setScreenOrientation";
String GET_SCREEN_ORIENTATION = "getScreenOrientation";
String SET_SCREEN_ROTATION = "setScreenRotation";
String GET_SCREEN_ROTATION = "getScreenRotation";

String ACTION_CHAIN = "actionChain";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public JsonHttpCommandCodec() {

defineCommand(GET_SCREEN_ORIENTATION, get("/session/:sessionId/orientation"));
defineCommand(SET_SCREEN_ORIENTATION, post("/session/:sessionId/orientation"));
defineCommand(GET_SCREEN_ROTATION, get("/session/:sessionId/rotation"));
defineCommand(SET_SCREEN_ROTATION, post("/session/:sessionId/rotation"));

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

0 comments on commit af490d9

Please sign in to comment.