Skip to content

Commit

Permalink
Clean up the Augmenter
Browse files Browse the repository at this point in the history
The Augmenter provides a mechanism to enhance classes dynamically at
runtime, but the existing implementation had a number of problems:

 1. It wasn't clear which version people should use.
 2. Neither version properly handled "reflexive" calls, which made
    implementing new locating mechanisms harder.

This diff cleans that up. It does so by removing the JRE Proxy-based
implementation (sadly --- I'd much rather we didn't have the bytebuddy
dependency as it's very heavy) and cleaning out the implementation.

The key interfaces and methods were marked as `Beta` or otherwise had
notices that they were liable to change without notice, so although
this isn't strictly in the spirit of the rest of the Se4 release,
it'll get us over the hump.
  • Loading branch information
shs96c committed Jun 9, 2020
1 parent dae6db2 commit bc75339
Show file tree
Hide file tree
Showing 18 changed files with 598 additions and 1,106 deletions.
23 changes: 6 additions & 17 deletions java/client/src/org/openqa/selenium/devtools/DevToolsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,31 @@

import com.google.auto.service.AutoService;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.AugmenterProvider;
import org.openqa.selenium.remote.InterfaceImplementation;
import org.openqa.selenium.remote.ExecuteMethod;

import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;

@AutoService(AugmenterProvider.class)
public class DevToolsProvider implements AugmenterProvider {
public class DevToolsProvider implements AugmenterProvider<HasDevTools> {

@Override
public Predicate<Capabilities> isApplicable() {
return caps -> getCdpUrl(caps) != null;
}

@Override
public Class<?> getDescribedInterface() {
public Class<HasDevTools> getDescribedInterface() {
return HasDevTools.class;
}

@Override
public InterfaceImplementation getImplementation(Object value) {
Require.argument("Implementation", value).instanceOf(Capabilities.class);
public HasDevTools getImplementation(Capabilities caps, ExecuteMethod executeMethod) {
Optional<DevTools> devTools = SeleniumCdpConnection.create(caps).map(DevTools::new);

Capabilities caps = (Capabilities) value;
Optional<DevTools> devTools = SeleniumCdpConnection.create(caps)
.map(DevTools::new);

return (executeMethod, self, method, args) -> {
if (!"getDevTools".equals(method.getName())) {
throw new IllegalStateException("Unexpected call to " + method);
}

return devTools.orElseThrow(() -> new IllegalStateException("Unable to create connection to " + caps));
};
return () -> devTools.orElseThrow(() -> new IllegalStateException("Unable to create connection to " + caps));
}

private String getCdpUrl(Capabilities caps) {
Expand Down
73 changes: 42 additions & 31 deletions java/client/src/org/openqa/selenium/remote/AddRotatable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,56 @@

package org.openqa.selenium.remote;

import com.google.common.collect.ImmutableMap;

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

public class AddRotatable implements AugmenterProvider {
import java.util.function.Predicate;

import static org.openqa.selenium.remote.CapabilityType.ROTATABLE;

public class AddRotatable implements AugmenterProvider<Rotatable> {

@Override
public Class<?> getDescribedInterface() {
public Predicate<Capabilities> isApplicable() {
return caps -> caps.is(ROTATABLE);
}

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

@Override
public InterfaceImplementation getImplementation(Object value) {
return (executeMethod, self, method, args) -> {
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 = executeMethod.execute(DriverCommand.GET_SCREEN_ROTATION, null);
break;
default:
throw new IllegalArgumentException(method.getName() + ", Not defined in rotatable interface");
}
return response;
};
public Rotatable getImplementation(Capabilities capabilities, ExecuteMethod executeMethod) {
return new RemoteRotatable(executeMethod);
}

// @Override
// public InterfaceImplementation getImplementation(Object value) {
// return (executeMethod, self, method, args) -> {
// 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 = executeMethod.execute(DriverCommand.GET_SCREEN_ROTATION, null);
// break;
// default:
// throw new IllegalArgumentException(method.getName() + ", Not defined in rotatable interface");
// }
// return response;
// };
// }

}
Loading

0 comments on commit bc75339

Please sign in to comment.