@@ -9,6 +9,7 @@ import KeyTable from '../core/input/keysym.js';
9
9
import legacyCrypto from '../core/crypto/crypto.js' ;
10
10
11
11
import FakeWebSocket from './fake.websocket.js' ;
12
+ import WakeLockManager from '../core/util/wakelock.js' ;
12
13
13
14
function push8 ( arr , num ) {
14
15
"use strict" ;
@@ -5148,6 +5149,102 @@ describe('Remote Frame Buffer protocol client', function () {
5148
5149
expect ( RFB . messages . clientEncodings . getCall ( 0 ) . args [ 1 ] ) . to . include ( encodings . pseudoEncodingCompressLevel0 + newCompression ) ;
5149
5150
} ) ;
5150
5151
} ) ;
5152
+
5153
+ describe ( 'wakelock setting' , function ( ) {
5154
+ let client ;
5155
+
5156
+ beforeEach ( function ( ) {
5157
+ sinon . spy ( WakeLockManager . prototype , "acquire" ) ;
5158
+ sinon . spy ( WakeLockManager . prototype , "release" ) ;
5159
+
5160
+ client = makeRFB ( ) ;
5161
+ } ) ;
5162
+
5163
+ afterEach ( function ( ) {
5164
+ WakeLockManager . prototype . acquire . restore ( ) ;
5165
+ WakeLockManager . prototype . release . restore ( ) ;
5166
+ } ) ;
5167
+
5168
+ it ( 'should acquire wakelock when connected' , function ( ) {
5169
+ expect ( WakeLockManager . prototype . acquire ) . to . not . have . been . called ;
5170
+
5171
+ client . requestLocalWakelock = true ;
5172
+ expect ( WakeLockManager . prototype . acquire ) . to . have . been . calledOnce ;
5173
+ } ) ;
5174
+
5175
+ it ( 'should acquire wakelock after connection' , function ( ) {
5176
+ expect ( WakeLockManager . prototype . acquire ) . to . not . have . been . called ;
5177
+ client . _rfbConnectionState = 'connecting' ;
5178
+
5179
+ client . requestLocalWakelock = true ;
5180
+ expect ( WakeLockManager . prototype . acquire ) . to . not . have . been . called ;
5181
+
5182
+ client . _updateConnectionState ( 'connected' ) ;
5183
+ expect ( WakeLockManager . prototype . acquire ) . to . have . been . calledOnce ;
5184
+ } ) ;
5185
+
5186
+ it ( 'should release wakelock when disabled' , function ( ) {
5187
+ client . requestLocalWakelock = true ;
5188
+ expect ( WakeLockManager . prototype . acquire ) . to . have . been . calledOnce ;
5189
+ expect ( WakeLockManager . prototype . release ) . to . not . have . been . called ;
5190
+
5191
+ client . requestLocalWakelock = false ;
5192
+ expect ( WakeLockManager . prototype . release ) . to . have . been . calledOnce ;
5193
+ } ) ;
5194
+
5195
+ it ( 'should release wakelock when disconnected' , function ( ) {
5196
+ client . requestLocalWakelock = true ;
5197
+ expect ( WakeLockManager . prototype . acquire ) . to . have . been . calledOnce ;
5198
+ expect ( WakeLockManager . prototype . release ) . to . not . have . been . called ;
5199
+
5200
+ client . disconnect ( ) ;
5201
+ expect ( WakeLockManager . prototype . release ) . to . have . been . calledOnce ;
5202
+ } ) ;
5203
+
5204
+ it ( 'should behave sensibly with non-boolean values' , function ( ) {
5205
+ // Client starts with requestLocakWakelock = false, setting it to
5206
+ // the same value should have no effect.
5207
+ client . requestLocalWakelock = false ;
5208
+ expect ( client . requestLocalWakelock ) . to . be . false ;
5209
+ expect ( WakeLockManager . prototype . acquire ) . to . not . have . been . called ;
5210
+ expect ( WakeLockManager . prototype . release ) . to . not . have . been . called ;
5211
+
5212
+ // Setting it to something else falsely should have no effect.
5213
+ client . requestLocalWakelock = null ;
5214
+ expect ( client . requestLocalWakelock ) . to . be . false ;
5215
+ expect ( WakeLockManager . prototype . acquire ) . to . not . have . been . called ;
5216
+ expect ( WakeLockManager . prototype . release ) . to . not . have . been . called ;
5217
+
5218
+ // Setting it to something else falsely should have no effect.
5219
+ client . requestLocalWakelock = undefined ;
5220
+ expect ( client . requestLocalWakelock ) . to . be . false ;
5221
+ expect ( WakeLockManager . prototype . acquire ) . to . not . have . been . called ;
5222
+ expect ( WakeLockManager . prototype . release ) . to . not . have . been . called ;
5223
+
5224
+ // Switching to something true should trigger a single call to
5225
+ // acquire.
5226
+ client . requestLocalWakelock = true ;
5227
+ expect ( client . requestLocalWakelock ) . to . be . true ;
5228
+ expect ( WakeLockManager . prototype . acquire ) . to . have . been . calledOnce ;
5229
+ expect ( WakeLockManager . prototype . release ) . to . not . have . been . called ;
5230
+
5231
+ WakeLockManager . prototype . acquire . resetHistory ( ) ;
5232
+
5233
+ // Switching to something else trueish should have no effect.
5234
+ client . requestLocalWakelock = "some-value" ;
5235
+ expect ( client . requestLocalWakelock ) . to . be . true ;
5236
+ expect ( WakeLockManager . prototype . acquire ) . to . not . have . been . called ;
5237
+ expect ( WakeLockManager . prototype . release ) . to . not . have . been . called ;
5238
+
5239
+ // Validate that switching from a trueish value to a falseish value
5240
+ // works.
5241
+ client . requestLocalWakelock = null ;
5242
+ expect ( client . requestLocalWakelock ) . to . be . false ;
5243
+ expect ( WakeLockManager . prototype . acquire ) . to . not . have . been . called ;
5244
+ expect ( WakeLockManager . prototype . release ) . to . have . been . calledOnce ;
5245
+ } ) ;
5246
+
5247
+ } ) ;
5151
5248
} ) ;
5152
5249
5153
5250
describe ( 'RFB messages' , function ( ) {
0 commit comments