Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions java/src/org/openqa/selenium/bidi/emulation/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("//java:defs.bzl", "java_library")

java_library(
name = "emulation",
srcs = glob(
[
"*.java",
],
),
visibility = [
"//java/src/org/openqa/selenium/bidi:__subpackages__",
"//java/src/org/openqa/selenium/remote:__pkg__",
"//java/test/org/openqa/selenium/bidi:__subpackages__",
"//java/test/org/openqa/selenium/grid:__subpackages__",
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/bidi",
"//java/src/org/openqa/selenium/bidi/browsingcontext",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote/http",
],
)
46 changes: 46 additions & 0 deletions java/src/org/openqa/selenium/bidi/emulation/Emulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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.bidi.emulation;

import java.util.Map;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Command;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.internal.Require;

public class Emulation {
private final BiDi bidi;

public Emulation(WebDriver driver) {
Require.nonNull("WebDriver", driver);

if (!(driver instanceof HasBiDi)) {
throw new IllegalArgumentException("WebDriver must implement BiDi interface");
}

this.bidi = ((HasBiDi) driver).getBiDi();
}

public Map<String, Object> setGeolocationOverride(SetGeolocationOverrideParameters parameters) {
Require.nonNull("SetGeolocationOverride parameters", parameters);

return bidi.send(
new Command<>("emulation.setGeolocationOverride", parameters.toMap(), Map.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// 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.bidi.emulation;

import java.util.HashMap;
import java.util.Map;

public class GeolocationCoordinates {
private final Map<String, Object> map = new HashMap<>();

public GeolocationCoordinates(double latitude, double longitude) {
if (latitude < -90.0 || latitude > 90.0) {
throw new IllegalArgumentException("Latitude must be between -90.0 and 90.0");
}
if (longitude < -180.0 || longitude > 180.0) {
throw new IllegalArgumentException("Longitude must be between -180.0 and 180.0");
}

map.put("latitude", latitude);
map.put("longitude", longitude);
map.put("accuracy", 1.0); // Default accuracy
}

public GeolocationCoordinates accuracy(double accuracy) {
if (accuracy < 0.0) {
throw new IllegalArgumentException("Accuracy must be >= 0.0");
}
map.put("accuracy", accuracy);
return this;
}

public GeolocationCoordinates altitude(Double altitude) {
if (altitude != null) {
map.put("altitude", altitude);
}
return this;
}

public GeolocationCoordinates altitudeAccuracy(Double altitudeAccuracy) {
if (altitudeAccuracy != null) {
if (!map.containsKey("altitude")) {
throw new IllegalArgumentException("altitudeAccuracy cannot be set without altitude");
}
if (altitudeAccuracy < 0.0) {
throw new IllegalArgumentException("Altitude accuracy must be >= 0.0");
}
map.put("altitudeAccuracy", altitudeAccuracy);
}
return this;
}

public GeolocationCoordinates heading(Double heading) {
if (heading != null) {
if (heading < 0.0 || heading >= 360.0) {
throw new IllegalArgumentException("Heading must be between 0.0 and 360.0");
}
map.put("heading", heading);
}
return this;
}

public GeolocationCoordinates speed(Double speed) {
if (speed != null) {
if (speed < 0.0) {
throw new IllegalArgumentException("Speed must be >= 0.0");
}
map.put("speed", speed);
}
return this;
}

public double getLatitude() {
return (Double) map.get("latitude");
}

public double getLongitude() {
return (Double) map.get("longitude");
}

public double getAccuracy() {
return (Double) map.get("accuracy");
}

public Double getAltitude() {
return (Double) map.get("altitude");
}

public Double getAltitudeAccuracy() {
return (Double) map.get("altitudeAccuracy");
}

public Double getHeading() {
return (Double) map.get("heading");
}

public Double getSpeed() {
return (Double) map.get("speed");
}

public Map<String, Object> toMap() {
return Map.copyOf(map);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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.bidi.emulation;

import java.util.Map;

public class GeolocationPositionError {

public Map<String, Object> toMap() {
String type = "positionUnavailable";
return Map.of("type", type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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.bidi.emulation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SetGeolocationOverrideParameters {
private final GeolocationCoordinates coordinates;
private final GeolocationPositionError error;
private final List<String> contexts;
private final List<String> userContexts;

public SetGeolocationOverrideParameters(
GeolocationCoordinates coordinates,
GeolocationPositionError error,
List<String> contexts,
List<String> userContexts) {

this.coordinates = coordinates;
this.error = error;
this.contexts = contexts;
this.userContexts = userContexts;

if (this.coordinates != null && this.error != null) {
throw new IllegalArgumentException("Cannot specify both coordinates and error");
}
if (this.contexts != null && this.userContexts != null) {
throw new IllegalArgumentException("Cannot specify both contexts and userContexts");
}

if (this.contexts == null && this.userContexts == null) {
throw new IllegalArgumentException("Must specify either contexts or userContexts");
}
}

public SetGeolocationOverrideParameters(
GeolocationCoordinates coordinates, GeolocationPositionError error) {
this(coordinates, error, null, null);
}

public Map<String, Object> toMap() {
Map<String, Object> param = new HashMap<>();

if (this.coordinates != null) {
param.put("coordinates", this.coordinates.toMap());
}

if (this.error != null) {
param.put("error", this.error.toMap());
}

if (this.contexts != null) {
param.put("contexts", this.contexts);
} else {
param.put("userContexts", this.userContexts);
}

return Map.copyOf(param);
}
}
1 change: 1 addition & 0 deletions java/src/org/openqa/selenium/bidi/module/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ java_library(
"//java/src/org/openqa/selenium/bidi",
"//java/src/org/openqa/selenium/bidi/browser",
"//java/src/org/openqa/selenium/bidi/browsingcontext",
"//java/src/org/openqa/selenium/bidi/emulation",
"//java/src/org/openqa/selenium/bidi/log",
"//java/src/org/openqa/selenium/bidi/network",
"//java/src/org/openqa/selenium/bidi/permissions",
Expand Down
1 change: 1 addition & 0 deletions java/src/org/openqa/selenium/remote/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ java_export(
"//java/src/org/openqa/selenium/bidi",
"//java/src/org/openqa/selenium/bidi:augmenter",
"//java/src/org/openqa/selenium/bidi/browsingcontext",
"//java/src/org/openqa/selenium/bidi/emulation",
"//java/src/org/openqa/selenium/bidi/log",
"//java/src/org/openqa/selenium/bidi/module",
"//java/src/org/openqa/selenium/bidi/network",
Expand Down
37 changes: 37 additions & 0 deletions java/test/org/openqa/selenium/bidi/emulation/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "JUNIT5_DEPS", "java_selenium_test_suite")

java_selenium_test_suite(
name = "large-tests",
size = "large",
srcs = glob(["*Test.java"]),
browsers = [
"firefox",
"chrome",
"edge",
],
tags = [
"selenium-remote",
],
deps = [
"//java/src/org/openqa/selenium/bidi",
"//java/src/org/openqa/selenium/bidi/browsingcontext",
"//java/src/org/openqa/selenium/bidi/emulation",
"//java/src/org/openqa/selenium/bidi/log",
"//java/src/org/openqa/selenium/bidi/module",
"//java/src/org/openqa/selenium/bidi/network",
"//java/src/org/openqa/selenium/bidi/script",
"//java/src/org/openqa/selenium/firefox",
"//java/src/org/openqa/selenium/grid/security",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote",
"//java/src/org/openqa/selenium/support",
"//java/test/org/openqa/selenium/environment",
"//java/test/org/openqa/selenium/testing:annotations",
"//java/test/org/openqa/selenium/testing:test-base",
"//java/test/org/openqa/selenium/testing/drivers",
artifact("com.google.guava:guava"),
artifact("org.junit.jupiter:junit-jupiter-api"),
artifact("org.assertj:assertj-core"),
] + JUNIT5_DEPS,
)
Loading
Loading