Skip to content

Commit bb57826

Browse files
committed
[java][BiDi] implement browsingContext.historyUpdated
1 parent 6a58efb commit bb57826

File tree

2 files changed

+113
-4
lines changed

2 files changed

+113
-4
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.bidi.browsingcontext;
19+
20+
import static java.util.Collections.unmodifiableMap;
21+
22+
import java.util.Map;
23+
import java.util.Optional;
24+
import java.util.TreeMap;
25+
import org.openqa.selenium.json.JsonInput;
26+
27+
public class HistoryUpdated {
28+
29+
private final String browsingContextId;
30+
31+
private final int timestamp;
32+
33+
private final String url;
34+
35+
private HistoryUpdated(String browsingContextId, int timestamp, String url) {
36+
this.browsingContextId = browsingContextId;
37+
this.timestamp = timestamp;
38+
this.url = url;
39+
}
40+
41+
public static HistoryUpdated fromJson(JsonInput input) {
42+
String browsingContextId = null;
43+
int timestamp = 0;
44+
String url = null;
45+
46+
input.beginObject();
47+
while (input.hasNext()) {
48+
switch (input.nextName()) {
49+
case "context":
50+
browsingContextId = input.read(String.class);
51+
break;
52+
53+
case "timestamp":
54+
timestamp = input.read(int.class);
55+
break;
56+
57+
case "url":
58+
url = input.read(String.class);
59+
break;
60+
61+
default:
62+
input.skipValue();
63+
break;
64+
}
65+
}
66+
67+
input.endObject();
68+
69+
return new HistoryUpdated(browsingContextId, timestamp, url);
70+
}
71+
72+
public String getBrowsingContextId() {
73+
return browsingContextId;
74+
}
75+
76+
public int getTimestamp() {
77+
return timestamp;
78+
}
79+
80+
public String getUrl() {
81+
return url;
82+
}
83+
84+
private Map<String, Object> toJson() {
85+
Map<String, Object> toReturn = new TreeMap<>();
86+
87+
toReturn.put("browsingContextId", this.getBrowsingContextId());
88+
toReturn.put("timestamp", this.getTimestamp());
89+
toReturn.put("url", this.getUrl());
90+
91+
return unmodifiableMap(toReturn);
92+
}
93+
}

java/src/org/openqa/selenium/bidi/module/BrowsingContextInspector.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@
2828
import org.openqa.selenium.bidi.BiDi;
2929
import org.openqa.selenium.bidi.Event;
3030
import org.openqa.selenium.bidi.HasBiDi;
31-
import org.openqa.selenium.bidi.browsingcontext.BrowsingContextInfo;
32-
import org.openqa.selenium.bidi.browsingcontext.NavigationInfo;
33-
import org.openqa.selenium.bidi.browsingcontext.UserPromptClosed;
34-
import org.openqa.selenium.bidi.browsingcontext.UserPromptOpened;
31+
import org.openqa.selenium.bidi.browsingcontext.*;
3532
import org.openqa.selenium.internal.Require;
3633
import org.openqa.selenium.json.Json;
3734
import org.openqa.selenium.json.JsonInput;
@@ -88,6 +85,16 @@ public class BrowsingContextInspector implements AutoCloseable {
8885
}
8986
});
9087

88+
private final Event<HistoryUpdated> historyUpdated =
89+
new Event<>(
90+
"browsingContext.historyUpdated",
91+
params -> {
92+
try (StringReader reader = new StringReader(JSON.toJson(params));
93+
JsonInput input = JSON.newInput(reader)) {
94+
return input.read(HistoryUpdated.class);
95+
}
96+
});
97+
9198
public BrowsingContextInspector(WebDriver driver) {
9299
this(new HashSet<>(), driver);
93100
}
@@ -172,6 +179,14 @@ public void onUserPromptOpened(Consumer<UserPromptOpened> consumer) {
172179
}
173180
}
174181

182+
public void onHistoryUpdated(Consumer<HistoryUpdated> consumer) {
183+
if (browsingContextIds.isEmpty()) {
184+
this.bidi.addListener(historyUpdated, consumer);
185+
} else {
186+
this.bidi.addListener(browsingContextIds, historyUpdated, consumer);
187+
}
188+
}
189+
175190
private void addNavigationEventListener(String name, Consumer<NavigationInfo> consumer) {
176191
Event<NavigationInfo> navigationEvent = new Event<>(name, navigationInfoMapper);
177192

@@ -190,6 +205,7 @@ public void close() {
190205
this.bidi.clearListener(browsingContextDestroyed);
191206
this.bidi.clearListener(userPromptOpened);
192207
this.bidi.clearListener(userPromptClosed);
208+
this.bidi.clearListener(historyUpdated);
193209

194210
navigationEventSet.forEach(this.bidi::clearListener);
195211
}

0 commit comments

Comments
 (0)