Skip to content

Commit abeb866

Browse files
committed
[java] Use Java Map instead of Guava Immutable Map in BiDi package
1 parent 889a636 commit abeb866

File tree

4 files changed

+37
-53
lines changed

4 files changed

+37
-53
lines changed

java/src/org/openqa/selenium/bidi/BiDi.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
package org.openqa.selenium.bidi;
1919

20-
import com.google.common.collect.ImmutableMap;
2120
import java.io.Closeable;
2221
import java.time.Duration;
2322
import java.util.Collections;
23+
import java.util.Map;
2424
import java.util.Set;
2525
import java.util.function.Consumer;
2626
import org.openqa.selenium.internal.Require;
@@ -60,8 +60,7 @@ public <X> void addListener(Event<X> event, Consumer<X> handler) {
6060

6161
send(
6262
new Command<>(
63-
"session.subscribe",
64-
ImmutableMap.of("events", Collections.singletonList(event.getMethod()))));
63+
"session.subscribe", Map.of("events", Collections.singletonList(event.getMethod()))));
6564

6665
connection.addListener(event, handler);
6766
}
@@ -74,7 +73,7 @@ <X> void addListener(String browsingContextId, Event<X> event, Consumer<X> handl
7473
send(
7574
new Command<>(
7675
"session.subscribe",
77-
ImmutableMap.of(
76+
Map.of(
7877
"contexts",
7978
Collections.singletonList(browsingContextId),
8079
"events",
@@ -91,7 +90,7 @@ <X> void addListener(Set<String> browsingContextIds, Event<X> event, Consumer<X>
9190
send(
9291
new Command<>(
9392
"session.subscribe",
94-
ImmutableMap.of(
93+
Map.of(
9594
"contexts",
9695
browsingContextIds,
9796
"events",
@@ -109,7 +108,7 @@ public <X> void clearListener(Event<X> event) {
109108
send(
110109
new Command<>(
111110
"session.unsubscribe",
112-
ImmutableMap.of("events", Collections.singletonList(event.getMethod()))));
111+
Map.of("events", Collections.singletonList(event.getMethod()))));
113112

114113
connection.clearListener(event);
115114
}

java/src/org/openqa/selenium/bidi/Command.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.openqa.selenium.bidi;
1919

20-
import com.google.common.collect.ImmutableMap;
2120
import java.lang.reflect.Type;
2221
import java.util.Map;
2322
import java.util.function.Function;
@@ -49,7 +48,7 @@ public Command(
4948
Function<JsonInput, X> mapper,
5049
boolean sendsResponse) {
5150
this.method = Require.nonNull("Method name", method);
52-
this.params = ImmutableMap.copyOf(Require.nonNull("Command parameters", params));
51+
this.params = Map.copyOf(Require.nonNull("Command parameters", params));
5352
this.mapper = Require.nonNull("Mapper for result", mapper);
5453
this.sendsResponse = sendsResponse;
5554
}

java/src/org/openqa/selenium/bidi/Connection.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static org.openqa.selenium.remote.http.HttpMethod.GET;
2424

2525
import com.google.common.collect.HashMultimap;
26-
import com.google.common.collect.ImmutableMap;
2726
import com.google.common.collect.Multimap;
2827
import java.io.Closeable;
2928
import java.io.StringReader;
@@ -136,14 +135,15 @@ public <X> CompletableFuture<X> send(Command<X> command) {
136135
}));
137136
}
138137

139-
ImmutableMap.Builder<String, Object> serialized = ImmutableMap.builder();
140-
serialized.put("id", id);
141-
serialized.put("method", command.getMethod());
142-
serialized.put("params", command.getParams());
138+
Map<String, Object> serialized =
139+
Map.of(
140+
"id", id,
141+
"method", command.getMethod(),
142+
"params", command.getParams());
143143

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

225225
eventCallbacks.clear();

java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.openqa.selenium.bidi.browsingcontext;
1919

20-
import com.google.common.collect.ImmutableMap;
2120
import java.lang.reflect.Type;
2221
import java.util.ArrayList;
2322
import java.util.HashMap;
@@ -110,46 +109,42 @@ public String getId() {
110109
private String create(WindowType type) {
111110
return this.bidi.send(
112111
new Command<>(
113-
"browsingContext.create",
114-
ImmutableMap.of("type", type.toString()),
115-
browsingContextIdMapper));
112+
"browsingContext.create", Map.of("type", type.toString()), browsingContextIdMapper));
116113
}
117114

118115
private String create(WindowType type, String referenceContext) {
119116
return this.bidi.send(
120117
new Command<>(
121118
"browsingContext.create",
122-
ImmutableMap.of("type", type.toString(), "referenceContext", referenceContext),
119+
Map.of("type", type.toString(), "referenceContext", referenceContext),
123120
browsingContextIdMapper));
124121
}
125122

126123
public NavigationResult navigate(String url) {
127124
return this.bidi.send(
128125
new Command<>(
129-
"browsingContext.navigate",
130-
ImmutableMap.of(CONTEXT, id, "url", url),
131-
navigationInfoMapper));
126+
"browsingContext.navigate", Map.of(CONTEXT, id, "url", url), navigationInfoMapper));
132127
}
133128

134129
public NavigationResult navigate(String url, ReadinessState readinessState) {
135130
return this.bidi.send(
136131
new Command<>(
137132
"browsingContext.navigate",
138-
ImmutableMap.of(CONTEXT, id, "url", url, "wait", readinessState.toString()),
133+
Map.of(CONTEXT, id, "url", url, "wait", readinessState.toString()),
139134
navigationInfoMapper));
140135
}
141136

142137
public List<BrowsingContextInfo> getTree() {
143138
return this.bidi.send(
144139
new Command<>(
145-
"browsingContext.getTree", ImmutableMap.of("root", id), browsingContextInfoListMapper));
140+
"browsingContext.getTree", Map.of("root", id), browsingContextInfoListMapper));
146141
}
147142

148143
public List<BrowsingContextInfo> getTree(int maxDepth) {
149144
return this.bidi.send(
150145
new Command<>(
151146
"browsingContext.getTree",
152-
ImmutableMap.of(
147+
Map.of(
153148
"root", id,
154149
"maxDepth", maxDepth),
155150
browsingContextInfoListMapper));
@@ -161,65 +156,56 @@ public List<BrowsingContextInfo> getTopLevelContexts() {
161156
}
162157

163158
public NavigationResult reload() {
164-
return this.bidi.send(
165-
new Command<>(RELOAD, ImmutableMap.of(CONTEXT, id), navigationInfoMapper));
159+
return this.bidi.send(new Command<>(RELOAD, Map.of(CONTEXT, id), navigationInfoMapper));
166160
}
167161

168162
// Yet to be implemented by browser vendors
169163
private NavigationResult reload(boolean ignoreCache) {
170164
return this.bidi.send(
171165
new Command<>(
172-
RELOAD,
173-
ImmutableMap.of(CONTEXT, id, "ignoreCache", ignoreCache),
174-
navigationInfoMapper));
166+
RELOAD, Map.of(CONTEXT, id, "ignoreCache", ignoreCache), navigationInfoMapper));
175167
}
176168

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

187177
// Yet to be implemented by browser vendors
188178
private NavigationResult reload(boolean ignoreCache, ReadinessState readinessState) {
189179
return this.bidi.send(
190180
new Command<>(
191181
RELOAD,
192-
ImmutableMap.of(
193-
CONTEXT, id, "ignoreCache", ignoreCache, "wait", readinessState.toString()),
182+
Map.of(CONTEXT, id, "ignoreCache", ignoreCache, "wait", readinessState.toString()),
194183
navigationInfoMapper));
195184
}
196185

197186
public void handleUserPrompt() {
198-
this.bidi.send(new Command<>(HANDLE_USER_PROMPT, ImmutableMap.of(CONTEXT, id)));
187+
this.bidi.send(new Command<>(HANDLE_USER_PROMPT, Map.of(CONTEXT, id)));
199188
}
200189

201190
public void handleUserPrompt(boolean accept) {
202-
this.bidi.send(
203-
new Command<>(HANDLE_USER_PROMPT, ImmutableMap.of(CONTEXT, id, "accept", accept)));
191+
this.bidi.send(new Command<>(HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "accept", accept)));
204192
}
205193

206194
public void handleUserPrompt(String userText) {
207-
this.bidi.send(
208-
new Command<>(HANDLE_USER_PROMPT, ImmutableMap.of(CONTEXT, id, "userText", userText)));
195+
this.bidi.send(new Command<>(HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "userText", userText)));
209196
}
210197

211198
public void handleUserPrompt(boolean accept, String userText) {
212199
this.bidi.send(
213200
new Command<>(
214-
HANDLE_USER_PROMPT,
215-
ImmutableMap.of(CONTEXT, id, "accept", accept, "userText", userText)));
201+
HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "accept", accept, "userText", userText)));
216202
}
217203

218204
public String captureScreenshot() {
219205
return this.bidi.send(
220206
new Command<>(
221207
"browsingContext.captureScreenshot",
222-
ImmutableMap.of(CONTEXT, id),
208+
Map.of(CONTEXT, id),
223209
jsonInput -> {
224210
Map<String, Object> result = jsonInput.read(Map.class);
225211
return (String) result.get("data");
@@ -230,11 +216,11 @@ public String captureBoxScreenshot(double x, double y, double width, double heig
230216
return this.bidi.send(
231217
new Command<>(
232218
"browsingContext.captureScreenshot",
233-
ImmutableMap.of(
219+
Map.of(
234220
CONTEXT,
235221
id,
236222
"clip",
237-
ImmutableMap.of(
223+
Map.of(
238224
"type", "viewport",
239225
"x", x,
240226
"y", y,
@@ -250,15 +236,15 @@ public String captureElementScreenshot(String elementId) {
250236
return this.bidi.send(
251237
new Command<>(
252238
"browsingContext.captureScreenshot",
253-
ImmutableMap.of(
239+
Map.of(
254240
CONTEXT,
255241
id,
256242
"clip",
257-
ImmutableMap.of(
243+
Map.of(
258244
"type",
259245
"element",
260246
"element",
261-
ImmutableMap.of("sharedId", elementId),
247+
Map.of("sharedId", elementId),
262248
"scrollIntoView",
263249
false)),
264250
jsonInput -> {
@@ -271,15 +257,15 @@ public String captureElementScreenshot(String elementId, boolean scrollIntoView)
271257
return this.bidi.send(
272258
new Command<>(
273259
"browsingContext.captureScreenshot",
274-
ImmutableMap.of(
260+
Map.of(
275261
CONTEXT,
276262
id,
277263
"clip",
278-
ImmutableMap.of(
264+
Map.of(
279265
"type",
280266
"element",
281267
"element",
282-
ImmutableMap.of("sharedId", elementId),
268+
Map.of("sharedId", elementId),
283269
"scrollIntoView",
284270
scrollIntoView)),
285271
jsonInput -> {
@@ -323,6 +309,6 @@ public void close() {
323309
// This might need more clean up actions once the behavior is defined.
324310
// Specially when last tab or window is closed.
325311
// Refer: https://github.com/w3c/webdriver-bidi/issues/187
326-
this.bidi.send(new Command<>("browsingContext.close", ImmutableMap.of(CONTEXT, id)));
312+
this.bidi.send(new Command<>("browsingContext.close", Map.of(CONTEXT, id)));
327313
}
328314
}

0 commit comments

Comments
 (0)