Skip to content

Commit 8f15565

Browse files
estefyscgmta
authored andcommitted
LibWeb: Add stub implementation for Navigator.getBattery()
Adds a stub that returns a cached rejected promise with a not yet implemented error. This converts battery-status/battery-promise-window.https.html from timeout to pass. Full implementation still needed.
1 parent e738864 commit 8f15565

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed

Libraries/LibWeb/HTML/Navigator.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ void Navigator::visit_edges(Cell::Visitor& visitor)
7676
visitor.visit(m_service_worker_container);
7777
visitor.visit(m_media_capabilities);
7878
visitor.visit(m_credentials);
79+
visitor.visit(m_battery_promise);
7980
}
8081

8182
GC::Ref<MimeTypeArray> Navigator::mime_types()
@@ -148,4 +149,31 @@ GC::Ref<MediaCapabilitiesAPI::MediaCapabilities> Navigator::media_capabilities()
148149
return *m_media_capabilities;
149150
}
150151

152+
// https://w3c.github.io/battery/#the-getbattery-method
153+
GC::Ref<WebIDL::Promise> Navigator::get_battery()
154+
{
155+
auto& realm = this->realm();
156+
157+
// FIXME: 1. If this.[[BatteryPromise]] is null, then set it to a new promise in this's relevant realm.
158+
if (!m_battery_promise) {
159+
WebIDL::SimpleException exception {
160+
WebIDL::SimpleExceptionType::TypeError,
161+
"Battery Status API is not yet implemented"sv
162+
};
163+
m_battery_promise = WebIDL::create_rejected_promise_from_exception(realm, move(exception));
164+
}
165+
166+
// FIXME: 2. If this's relevant global object's associated Document is not allowed to use the "battery"
167+
// policy-controlled feature, then reject this.[[BatteryPromise]] with a "NotAllowedError" DOMException.
168+
169+
// FIXME: 3. Otherwise:
170+
// 1. If this.[[BatteryManager]] is null, then set it to the result of creating a new BatteryManager
171+
// in this's relevant realm.
172+
173+
// 2. Resolve this.[[BatteryPromise]] with this.[[BatteryManager]].
174+
175+
// 4. Return this.[[BatteryPromise]].
176+
return *m_battery_promise;
177+
}
178+
151179
}

Libraries/LibWeb/HTML/Navigator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Navigator
6565
[[nodiscard]] GC::Ref<Serial::Serial> serial();
6666
[[nodiscard]] GC::Ref<UserActivation> user_activation();
6767
[[nodiscard]] GC::Ref<CredentialManagement::CredentialsContainer> credentials();
68+
[[nodiscard]] GC::Ref<WebIDL::Promise> get_battery();
6869

6970
GC::Ref<ServiceWorker::ServiceWorkerContainer> service_worker();
7071

@@ -108,6 +109,9 @@ class Navigator
108109

109110
// https://w3c.github.io/webappsec-credential-management/#framework-credential-management
110111
GC::Ptr<CredentialManagement::CredentialsContainer> m_credentials;
112+
113+
// https://w3c.github.io/battery/
114+
GC::Ptr<WebIDL::Promise> m_battery_promise;
111115
};
112116

113117
}

Libraries/LibWeb/HTML/Navigator.idl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ interface Navigator {
4646

4747
// https://w3c.github.io/webappsec-credential-management/#framework-credential-management
4848
[SecureContext, SameObject] readonly attribute CredentialsContainer credentials;
49+
50+
// https://w3c.github.io/battery/
51+
[SecureContext] Promise<BatteryManager> getBattery();
4952
};
5053

5154
// NOTE: As NavigatorContentUtils, NavigatorCookies, NavigatorPlugins, and NavigatorAutomationInformation
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Harness status: OK
2+
3+
Found 1 tests
4+
5+
1 Pass
6+
Pass window.open() makes a different Navigator object thus getting another battery promise
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>Battery Test: window.open() makes a different Navigator object</title>
4+
<link rel="author" title="Intel" href="http://www.intel.com">
5+
<link rel="help" href="https://www.w3.org/TR/battery-status/">
6+
<script src="../resources/testharness.js"></script>
7+
<script src="../resources/testharnessreport.js"></script>
8+
<style>
9+
#note {
10+
background-color: #fef1b5;
11+
border: solid 1px #cdab2d;
12+
padding: 5px;
13+
margin: 15px;
14+
display: block;
15+
}
16+
</style>
17+
<div id="note">
18+
Allow pop-up windows before running the tests.
19+
</div>
20+
<div id="log"></div>
21+
<script>
22+
async_test(function (t) {
23+
var win = window.open('resources/support-window-open.html');
24+
window.onmessage = t.step_func(function(e) {
25+
assert_array_equals(e.data, [false, false, true]);
26+
win.close();
27+
t.done();
28+
});
29+
}, 'window.open() makes a different Navigator object thus getting another battery promise');
30+
</script>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE HTML>
2+
<meta charset="utf-8">
3+
<script>
4+
var data = [
5+
navigator === window.opener.navigator,
6+
navigator.getBattery() === window.opener.navigator.getBattery(),
7+
navigator.getBattery() === navigator.getBattery()
8+
];
9+
window.opener.postMessage(data, '*');
10+
</script>

0 commit comments

Comments
 (0)