Skip to content

Commit e6a7987

Browse files
committed
[java][bidi] Ensure removing listeners does not cause an error
1 parent 393ba87 commit e6a7987

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ <X> void addListener(Set<String> browsingContextIds, Event<X> event, Consumer<X>
9393
public <X> void clearListener(Event<X> event) {
9494
Require.nonNull("Event to listen for", event);
9595

96-
send(new Command<>("session.unsubscribe",
97-
ImmutableMap.of("events", Collections.singletonList(event.getMethod()))));
98-
99-
connection.clearListener(event);
96+
// The browser throws an error if we try to unsubscribe an event that was not subscribed in the first place
97+
if (connection.isEventSubscribed(event)) {
98+
send(new Command<>(
99+
"session.unsubscribe",
100+
ImmutableMap.of("events", Collections.singletonList(event.getMethod()))));
101+
102+
connection.clearListener(event);
103+
}
100104
}
101105

102106
public void clearListeners() {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ public <X> void clearListener(Event<X> event) {
188188
}
189189
}
190190

191+
public <X> boolean isEventSubscribed(Event<X> event) {
192+
Lock lock = callbacksLock.writeLock();
193+
lock.lock();
194+
try {
195+
return eventCallbacks.containsKey(event);
196+
} finally {
197+
lock.unlock();
198+
}
199+
}
200+
191201
public void clearListeners() {
192202
Lock lock = callbacksLock.writeLock();
193203
lock.lock();

0 commit comments

Comments
 (0)