@@ -3,7 +3,7 @@ import {assert} from '../test/assert.ts'
33import { WakelockMock } from '../test/wakelock-mock.ts'
44import { Wakelock } from './wakelock.ts'
55
6- test ( 'Wakelock acquires and reacquires after browser release' , async ( ) => {
6+ test ( 'wakelock acquires and reacquires after browser release' , async ( ) => {
77 using mock = new WakelockMock ( )
88 const wakelock = new Wakelock ( )
99 wakelock . update ( )
@@ -15,7 +15,7 @@ test('Wakelock acquires and reacquires after browser release', async () => {
1515 assert . equal ( wakelock . locked , true )
1616} )
1717
18- test ( 'Wakelock stays unlocked when the browser rejects it' , async ( ) => {
18+ test ( 'wakelock stays unlocked when the browser rejects it' , async ( ) => {
1919 using mock = new WakelockMock ( )
2020 mock . rejection = Error ( 'rejected' )
2121 const wakelock = new Wakelock ( )
@@ -27,3 +27,53 @@ test('Wakelock stays unlocked when the browser rejects it', async () => {
2727 await Promise . resolve ( )
2828 assert . equal ( mock . reqs , 2 )
2929} )
30+
31+ test ( 'wakelock follows document focus' , async ( ) => {
32+ using mock = new WakelockMock ( )
33+ mock . focused = false
34+ const wakelock = new Wakelock ( )
35+ wakelock . update ( )
36+ await Promise . resolve ( )
37+ assert . equal ( mock . reqs , 0 )
38+ assert . equal ( wakelock . locked , false )
39+
40+ mock . focused = true
41+ wakelock . update ( )
42+ await Promise . resolve ( )
43+ assert . equal ( wakelock . locked , true )
44+
45+ mock . focused = false
46+ wakelock . update ( )
47+ assert . equal ( mock . releases , 1 )
48+ assert . equal ( wakelock . locked , false )
49+
50+ mock . focused = true
51+ wakelock . update ( )
52+ await Promise . resolve ( )
53+ assert . equal ( mock . reqs , 2 )
54+ assert . equal ( wakelock . locked , true )
55+ } )
56+
57+ test ( 'wakelock releases a pending acquisition after focus loss' , async ( ) => {
58+ using mock = new WakelockMock ( )
59+ const wakelock = new Wakelock ( )
60+ wakelock . update ( )
61+ mock . focused = false
62+ wakelock . update ( )
63+ await Promise . resolve ( )
64+ assert . equal ( mock . reqs , 1 )
65+ assert . equal ( mock . releases , 1 )
66+ assert . equal ( wakelock . locked , false )
67+ } )
68+
69+ test ( 'wakelock stays unlocked after browser release without focus' , async ( ) => {
70+ using mock = new WakelockMock ( )
71+ const wakelock = new Wakelock ( )
72+ wakelock . update ( )
73+ await Promise . resolve ( )
74+ mock . focused = false
75+ mock . sentinel ?. dispatchEvent ( new Event ( 'release' ) )
76+ await Promise . resolve ( )
77+ assert . equal ( mock . reqs , 1 )
78+ assert . equal ( wakelock . locked , false )
79+ } )
0 commit comments