Skip to content

Commit

Permalink
[java] Use Java Map instead of Guava Immutable Map in BiDi package
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Oct 10, 2023
1 parent 889a636 commit abeb866
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 53 deletions.
11 changes: 5 additions & 6 deletions java/src/org/openqa/selenium/bidi/BiDi.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.openqa.selenium.bidi;

import com.google.common.collect.ImmutableMap;
import java.io.Closeable;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import org.openqa.selenium.internal.Require;
Expand Down Expand Up @@ -60,8 +60,7 @@ public <X> void addListener(Event<X> event, Consumer<X> handler) {

send(
new Command<>(
"session.subscribe",
ImmutableMap.of("events", Collections.singletonList(event.getMethod()))));
"session.subscribe", Map.of("events", Collections.singletonList(event.getMethod()))));

connection.addListener(event, handler);
}
Expand All @@ -74,7 +73,7 @@ <X> void addListener(String browsingContextId, Event<X> event, Consumer<X> handl
send(
new Command<>(
"session.subscribe",
ImmutableMap.of(
Map.of(
"contexts",
Collections.singletonList(browsingContextId),
"events",
Expand All @@ -91,7 +90,7 @@ <X> void addListener(Set<String> browsingContextIds, Event<X> event, Consumer<X>
send(
new Command<>(
"session.subscribe",
ImmutableMap.of(
Map.of(
"contexts",
browsingContextIds,
"events",
Expand All @@ -109,7 +108,7 @@ public <X> void clearListener(Event<X> event) {
send(
new Command<>(
"session.unsubscribe",
ImmutableMap.of("events", Collections.singletonList(event.getMethod()))));
Map.of("events", Collections.singletonList(event.getMethod()))));

connection.clearListener(event);
}
Expand Down
3 changes: 1 addition & 2 deletions java/src/org/openqa/selenium/bidi/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.openqa.selenium.bidi;

import com.google.common.collect.ImmutableMap;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -49,7 +48,7 @@ public Command(
Function<JsonInput, X> mapper,
boolean sendsResponse) {
this.method = Require.nonNull("Method name", method);
this.params = ImmutableMap.copyOf(Require.nonNull("Command parameters", params));
this.params = Map.copyOf(Require.nonNull("Command parameters", params));
this.mapper = Require.nonNull("Mapper for result", mapper);
this.sendsResponse = sendsResponse;
}
Expand Down
14 changes: 7 additions & 7 deletions java/src/org/openqa/selenium/bidi/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static org.openqa.selenium.remote.http.HttpMethod.GET;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import java.io.Closeable;
import java.io.StringReader;
Expand Down Expand Up @@ -136,14 +135,15 @@ public <X> CompletableFuture<X> send(Command<X> command) {
}));
}

ImmutableMap.Builder<String, Object> serialized = ImmutableMap.builder();
serialized.put("id", id);
serialized.put("method", command.getMethod());
serialized.put("params", command.getParams());
Map<String, Object> serialized =
Map.of(
"id", id,
"method", command.getMethod(),
"params", command.getParams());

StringBuilder json = new StringBuilder();
try (JsonOutput out = JSON.newOutput(json).writeClassName(false)) {
out.write(serialized.build());
out.write(serialized);
}
LOG.log(getDebugLogLevel(), "-> {0}", json);
socket.sendText(json);
Expand Down Expand Up @@ -219,7 +219,7 @@ public void clearListeners() {
// will throw errors.
// Ideally, such errors should not prevent freeing up resources.
if (!underlyingSocketClosed.get()) {
send(new Command<>("session.unsubscribe", ImmutableMap.of("events", events)));
send(new Command<>("session.unsubscribe", Map.of("events", events)));
}

eventCallbacks.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.openqa.selenium.bidi.browsingcontext;

import com.google.common.collect.ImmutableMap;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -110,46 +109,42 @@ public String getId() {
private String create(WindowType type) {
return this.bidi.send(
new Command<>(
"browsingContext.create",
ImmutableMap.of("type", type.toString()),
browsingContextIdMapper));
"browsingContext.create", Map.of("type", type.toString()), browsingContextIdMapper));
}

private String create(WindowType type, String referenceContext) {
return this.bidi.send(
new Command<>(
"browsingContext.create",
ImmutableMap.of("type", type.toString(), "referenceContext", referenceContext),
Map.of("type", type.toString(), "referenceContext", referenceContext),
browsingContextIdMapper));
}

public NavigationResult navigate(String url) {
return this.bidi.send(
new Command<>(
"browsingContext.navigate",
ImmutableMap.of(CONTEXT, id, "url", url),
navigationInfoMapper));
"browsingContext.navigate", Map.of(CONTEXT, id, "url", url), navigationInfoMapper));
}

public NavigationResult navigate(String url, ReadinessState readinessState) {
return this.bidi.send(
new Command<>(
"browsingContext.navigate",
ImmutableMap.of(CONTEXT, id, "url", url, "wait", readinessState.toString()),
Map.of(CONTEXT, id, "url", url, "wait", readinessState.toString()),
navigationInfoMapper));
}

public List<BrowsingContextInfo> getTree() {
return this.bidi.send(
new Command<>(
"browsingContext.getTree", ImmutableMap.of("root", id), browsingContextInfoListMapper));
"browsingContext.getTree", Map.of("root", id), browsingContextInfoListMapper));
}

public List<BrowsingContextInfo> getTree(int maxDepth) {
return this.bidi.send(
new Command<>(
"browsingContext.getTree",
ImmutableMap.of(
Map.of(
"root", id,
"maxDepth", maxDepth),
browsingContextInfoListMapper));
Expand All @@ -161,65 +156,56 @@ public List<BrowsingContextInfo> getTopLevelContexts() {
}

public NavigationResult reload() {
return this.bidi.send(
new Command<>(RELOAD, ImmutableMap.of(CONTEXT, id), navigationInfoMapper));
return this.bidi.send(new Command<>(RELOAD, Map.of(CONTEXT, id), navigationInfoMapper));
}

// Yet to be implemented by browser vendors
private NavigationResult reload(boolean ignoreCache) {
return this.bidi.send(
new Command<>(
RELOAD,
ImmutableMap.of(CONTEXT, id, "ignoreCache", ignoreCache),
navigationInfoMapper));
RELOAD, Map.of(CONTEXT, id, "ignoreCache", ignoreCache), navigationInfoMapper));
}

// TODO: Handle timeouts in case of Readiness state "interactive" and "complete".
// Refer https://github.com/w3c/webdriver-bidi/issues/188
public NavigationResult reload(ReadinessState readinessState) {
return this.bidi.send(
new Command<>(
RELOAD,
ImmutableMap.of(CONTEXT, id, "wait", readinessState.toString()),
navigationInfoMapper));
RELOAD, Map.of(CONTEXT, id, "wait", readinessState.toString()), navigationInfoMapper));
}

// Yet to be implemented by browser vendors
private NavigationResult reload(boolean ignoreCache, ReadinessState readinessState) {
return this.bidi.send(
new Command<>(
RELOAD,
ImmutableMap.of(
CONTEXT, id, "ignoreCache", ignoreCache, "wait", readinessState.toString()),
Map.of(CONTEXT, id, "ignoreCache", ignoreCache, "wait", readinessState.toString()),
navigationInfoMapper));
}

public void handleUserPrompt() {
this.bidi.send(new Command<>(HANDLE_USER_PROMPT, ImmutableMap.of(CONTEXT, id)));
this.bidi.send(new Command<>(HANDLE_USER_PROMPT, Map.of(CONTEXT, id)));
}

public void handleUserPrompt(boolean accept) {
this.bidi.send(
new Command<>(HANDLE_USER_PROMPT, ImmutableMap.of(CONTEXT, id, "accept", accept)));
this.bidi.send(new Command<>(HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "accept", accept)));
}

public void handleUserPrompt(String userText) {
this.bidi.send(
new Command<>(HANDLE_USER_PROMPT, ImmutableMap.of(CONTEXT, id, "userText", userText)));
this.bidi.send(new Command<>(HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "userText", userText)));
}

public void handleUserPrompt(boolean accept, String userText) {
this.bidi.send(
new Command<>(
HANDLE_USER_PROMPT,
ImmutableMap.of(CONTEXT, id, "accept", accept, "userText", userText)));
HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "accept", accept, "userText", userText)));
}

public String captureScreenshot() {
return this.bidi.send(
new Command<>(
"browsingContext.captureScreenshot",
ImmutableMap.of(CONTEXT, id),
Map.of(CONTEXT, id),
jsonInput -> {
Map<String, Object> result = jsonInput.read(Map.class);
return (String) result.get("data");
Expand All @@ -230,11 +216,11 @@ public String captureBoxScreenshot(double x, double y, double width, double heig
return this.bidi.send(
new Command<>(
"browsingContext.captureScreenshot",
ImmutableMap.of(
Map.of(
CONTEXT,
id,
"clip",
ImmutableMap.of(
Map.of(
"type", "viewport",
"x", x,
"y", y,
Expand All @@ -250,15 +236,15 @@ public String captureElementScreenshot(String elementId) {
return this.bidi.send(
new Command<>(
"browsingContext.captureScreenshot",
ImmutableMap.of(
Map.of(
CONTEXT,
id,
"clip",
ImmutableMap.of(
Map.of(
"type",
"element",
"element",
ImmutableMap.of("sharedId", elementId),
Map.of("sharedId", elementId),
"scrollIntoView",
false)),
jsonInput -> {
Expand All @@ -271,15 +257,15 @@ public String captureElementScreenshot(String elementId, boolean scrollIntoView)
return this.bidi.send(
new Command<>(
"browsingContext.captureScreenshot",
ImmutableMap.of(
Map.of(
CONTEXT,
id,
"clip",
ImmutableMap.of(
Map.of(
"type",
"element",
"element",
ImmutableMap.of("sharedId", elementId),
Map.of("sharedId", elementId),
"scrollIntoView",
scrollIntoView)),
jsonInput -> {
Expand Down Expand Up @@ -323,6 +309,6 @@ public void close() {
// This might need more clean up actions once the behavior is defined.
// Specially when last tab or window is closed.
// Refer: https://github.com/w3c/webdriver-bidi/issues/187
this.bidi.send(new Command<>("browsingContext.close", ImmutableMap.of(CONTEXT, id)));
this.bidi.send(new Command<>("browsingContext.close", Map.of(CONTEXT, id)));
}
}

0 comments on commit abeb866

Please sign in to comment.