Add error state for wakelock testing.

Add an error state to the WakeLockManager state machine. This adds a
transition that can be detected from tests (it otherwise serves no
purpose, and the system immediatly transitions back into the released
state).
This commit is contained in:
Greg Darke 2025-09-12 12:18:22 +10:00
parent 8341fdf846
commit 077c54f312
1 changed files with 13 additions and 3 deletions

View File

@ -19,7 +19,7 @@ const _STATES = {
* Can transition to: * Can transition to:
* - AWAITING_VISIBLE: `acquire` called when document is hidden. * - AWAITING_VISIBLE: `acquire` called when document is hidden.
* - ACQUIRING: `acquire` called. * - ACQUIRING: `acquire` called.
* - RELEASED: `acquired` called when the api is not available. * - ERROR: `acquired` called when the api is not available.
*/ */
RELEASED: 'released', RELEASED: 'released',
/* Wake lock requested, waiting for browser. /* Wake lock requested, waiting for browser.
@ -27,7 +27,7 @@ const _STATES = {
* Can transition to: * Can transition to:
* - ACQUIRED: success * - ACQUIRED: success
* - ACQUIRING_WANT_RELEASE: `release` called while waiting * - ACQUIRING_WANT_RELEASE: `release` called while waiting
* - RELEASED: On error * - ERROR
*/ */
ACQUIRING: 'acquiring', ACQUIRING: 'acquiring',
/* Wake lock requested, release called, still waiting for browser. /* Wake lock requested, release called, still waiting for browser.
@ -51,6 +51,12 @@ const _STATES = {
* - RELEASED: when release is called. * - RELEASED: when release is called.
*/ */
AWAITING_VISIBLE: 'awaiting_visible', AWAITING_VISIBLE: 'awaiting_visible',
/* An error has occurred.
*
* Can transition to:
* - RELEASED: will happen immediately.
*/
ERROR: 'error',
}; };
export default class WakeLockManager { export default class WakeLockManager {
@ -77,6 +83,7 @@ export default class WakeLockManager {
case _STATES.ACQUIRING: case _STATES.ACQUIRING:
case _STATES.ACQUIRED: case _STATES.ACQUIRED:
break; break;
case _STATES.ERROR:
case _STATES.RELEASED: case _STATES.RELEASED:
if (document.hidden) { if (document.hidden) {
// We can not acquire the wakelock while the document is // We can not acquire the wakelock while the document is
@ -92,6 +99,7 @@ export default class WakeLockManager {
release() { release() {
switch (this._state) { switch (this._state) {
case _STATES.ERROR:
case _STATES.RELEASED: case _STATES.RELEASED:
case _STATES.ACQUIRING_WANT_RELEASE: case _STATES.ACQUIRING_WANT_RELEASE:
break; break;
@ -132,6 +140,7 @@ export default class WakeLockManager {
_acquireWakelockNow() { _acquireWakelockNow() {
if (!("wakeLock" in navigator)) { if (!("wakeLock" in navigator)) {
Log.Warn("Unable to request wakeLock, Browser does not have wakeLock api"); Log.Warn("Unable to request wakeLock, Browser does not have wakeLock api");
this._transitionTo(_STATES.ERROR);
this._transitionTo(_STATES.RELEASED); this._transitionTo(_STATES.RELEASED);
return; return;
} }
@ -139,6 +148,7 @@ export default class WakeLockManager {
.then(this._eventHandlers.wakelockAcquired) .then(this._eventHandlers.wakelockAcquired)
.catch((err) => { .catch((err) => {
Log.Warn("Error occurred while acquiring wakelock: " + err); Log.Warn("Error occurred while acquiring wakelock: " + err);
this._transitionTo(_STATES.ERROR);
this._transitionTo(_STATES.RELEASED); this._transitionTo(_STATES.RELEASED);
}); });
this._transitionTo(_STATES.ACQUIRING); this._transitionTo(_STATES.ACQUIRING);
@ -148,7 +158,7 @@ export default class WakeLockManager {
_wakelockAcquired(wakelock) { _wakelockAcquired(wakelock) {
if (this._state === _STATES.ACQUIRING_WANT_RELEASE) { if (this._state === _STATES.ACQUIRING_WANT_RELEASE) {
// We were requested to release the wakelock while we were trying to // We were requested to release the wakelock while we were trying to
// acquire it. Now that we have acquired it, immediatly release it. // acquire it. Now that we have acquired it, immediately release it.
wakelock.release(); wakelock.release();
this._transitionTo(_STATES.RELEASED); this._transitionTo(_STATES.RELEASED);
return; return;