Skip to content
Merged
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 @@ -25,6 +25,7 @@ java_library(
"//java/src/org/openqa/selenium/bidi/network",
"//java/src/org/openqa/selenium/bidi/permissions",
"//java/src/org/openqa/selenium/bidi/script",
"//java/src/org/openqa/selenium/bidi/speculation",
"//java/src/org/openqa/selenium/bidi/storage",
"//java/src/org/openqa/selenium/bidi/webextension",
"//java/src/org/openqa/selenium/json",
Expand Down
76 changes: 76 additions & 0 deletions java/src/org/openqa/selenium/bidi/module/SpeculationInspector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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.module;

import static java.util.Collections.emptySet;

import java.util.Collections;
import java.util.Set;
import java.util.function.Consumer;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Event;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.speculation.PrefetchStatusUpdatedParameters;
import org.openqa.selenium.bidi.speculation.Speculation;
import org.openqa.selenium.internal.Require;

public class SpeculationInspector implements AutoCloseable {
private final Event<PrefetchStatusUpdatedParameters> prefetchStatusUpdatedEvent;
private final Set<String> browsingContextIds;

private final BiDi bidi;

public SpeculationInspector(WebDriver driver) {
this(emptySet(), driver);
}

public SpeculationInspector(String browsingContextId, WebDriver driver) {
this(Collections.singleton(Require.nonNull("Browsing context id", browsingContextId)), driver);
}

public SpeculationInspector(Set<String> browsingContextIds, WebDriver driver) {
Require.nonNull("WebDriver", driver);
Require.nonNull("Browsing context id list", browsingContextIds);

if (!(driver instanceof HasBiDi)) {
throw new IllegalArgumentException("WebDriver instance must support BiDi protocol");
}

this.bidi = ((HasBiDi) driver).getBiDi();
this.browsingContextIds = browsingContextIds;
this.prefetchStatusUpdatedEvent = Speculation.prefetchStatusUpdated();
}

public long onPrefetchStatusUpdated(Consumer<PrefetchStatusUpdatedParameters> consumer) {
if (browsingContextIds.isEmpty()) {
return this.bidi.addListener(this.prefetchStatusUpdatedEvent, consumer);
} else {
return this.bidi.addListener(browsingContextIds, this.prefetchStatusUpdatedEvent, consumer);
}
}

public void removeListener(long subscriptionId) {
this.bidi.removeListener(subscriptionId);
}

@Override
public void close() {
this.bidi.clearListener(Speculation.prefetchStatusUpdated());
Comment thread
Delta456 marked this conversation as resolved.
}
}
24 changes: 24 additions & 0 deletions java/src/org/openqa/selenium/bidi/speculation/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "java_library")

java_library(
name = "speculation",
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/json",
"//java/src/org/openqa/selenium/remote/http",
artifact("org.jspecify:jspecify"),
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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.speculation;

import java.util.Map;

public class PrefetchStatusUpdatedParameters {

private final String context;
private final String url;
private final PreloadingStatus status;

private PrefetchStatusUpdatedParameters(String context, String url, PreloadingStatus status) {
this.context = context;
this.url = url;
this.status = status;
}

public static PrefetchStatusUpdatedParameters fromJson(Map<String, Object> params) {
String context = (String) params.get("context");
String url = (String) params.get("url");
String statusStr = (String) params.get("status");
PreloadingStatus status = PreloadingStatus.fromString(statusStr);

return new PrefetchStatusUpdatedParameters(context, url, status);
}

public String getContext() {
return context;
}

public String getUrl() {
return url;
}

public PreloadingStatus getStatus() {
return status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.speculation;

public enum PreloadingStatus {
PENDING("pending"),
READY("ready"),
SUCCESS("success"),
FAILURE("failure");

private final String status;

PreloadingStatus(String status) {
this.status = status;
}

@Override
public String toString() {
return status;
}

public static PreloadingStatus fromString(String status) {
for (PreloadingStatus s : PreloadingStatus.values()) {
if (s.status.equalsIgnoreCase(status)) {
return s;
}
}
throw new IllegalArgumentException("Unknown preloading status: " + status);
}
}
28 changes: 28 additions & 0 deletions java/src/org/openqa/selenium/bidi/speculation/Speculation.java
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.speculation;

import org.openqa.selenium.bidi.Event;

public class Speculation {
public static Event<PrefetchStatusUpdatedParameters> prefetchStatusUpdated() {
return new Event<>(
"speculation.prefetchStatusUpdated",
params -> PrefetchStatusUpdatedParameters.fromJson(params));
}
}
21 changes: 21 additions & 0 deletions java/src/org/openqa/selenium/bidi/speculation/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.

@NullMarked
package org.openqa.selenium.bidi.speculation;

import org.jspecify.annotations.NullMarked;
31 changes: 31 additions & 0 deletions java/test/org/openqa/selenium/bidi/speculation/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "BIDI_BROWSERS", "JUNIT5_DEPS", "java_selenium_test_suite")

java_selenium_test_suite(
name = "large-tests",
size = "large",
srcs = glob(["*Test.java"]),
browsers = BIDI_BROWSERS,
tags = [
"selenium-remote",
],
deps = [
"//java/src/org/openqa/selenium/bidi",
"//java/src/org/openqa/selenium/bidi/browsingcontext",
"//java/src/org/openqa/selenium/bidi/log",
"//java/src/org/openqa/selenium/bidi/module",
"//java/src/org/openqa/selenium/bidi/script",
"//java/src/org/openqa/selenium/bidi/speculation",
"//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("org.junit.jupiter:junit-jupiter-api"),
artifact("org.assertj:assertj-core"),
] + JUNIT5_DEPS,
)
Loading
Loading