@@ -3466,18 +3466,62 @@ describe('Remote Frame Buffer protocol client', function () {
3466
3466
} ) ;
3467
3467
} ) ;
3468
3468
3469
+ describe ( '_writeClipboard()' , function ( ) {
3470
+
3471
+ it ( 'calls clipboard.writeClipboard if async clipboard is available' , async function ( ) {
3472
+ client . _isAsyncClipboardAvailable = async ( ) => true ;
3473
+ client . _clipboard = { writeClipboard : sinon . stub ( ) . resolves ( ) } ;
3474
+ const text = 'text to system clipboard' ;
3475
+ await client . _writeClipboard ( text ) ;
3476
+ sinon . assert . calledOnceWithExactly ( client . _clipboard . writeClipboard , text ) ;
3477
+ } ) ;
3478
+
3479
+ it ( 'dispatches a clipboard event if async clipboard is unavailable' , async function ( ) {
3480
+ client . _isAsyncClipboardAvailable = async ( ) => false ;
3481
+ const text = 'text to clipboard textarea' ;
3482
+ const spyPromise = new Promise ( ( resolve ) => {
3483
+ const handler = ( e ) => {
3484
+ client . removeEventListener ( "clipboard" , handler ) ;
3485
+ resolve ( e ) ;
3486
+ } ;
3487
+ client . addEventListener ( 'clipboard' , handler ) ;
3488
+ } ) ;
3489
+ await client . _writeClipboard ( text ) ;
3490
+ const event = await spyPromise ;
3491
+ expect ( event . detail . text ) . to . equal ( text ) ;
3492
+ } ) ;
3493
+
3494
+ it ( 'does nothing in view only mode' , async function ( ) {
3495
+ client . _viewOnly = true ;
3496
+ client . _clipboard = { writeClipboard : sinon . stub ( ) } ;
3497
+ const text = 'text that will not be written' ;
3498
+ let called = false ;
3499
+ client . addEventListener ( 'clipboard' , ( ) => called = true ) ;
3500
+ await client . _writeClipboard ( text ) ;
3501
+ sinon . assert . notCalled ( client . _clipboard . writeClipboard ) ;
3502
+ expect ( called ) . to . be . false ;
3503
+ } ) ;
3504
+ } ) ;
3505
+
3469
3506
describe ( 'Normal clipboard handling receive' , function ( ) {
3470
- it ( 'should fire the clipboard callback with the retrieved text on ServerCutText' , function ( ) {
3507
+ it ( 'should fire the clipboard callback with the retrieved text on ServerCutText' , async function ( ) {
3508
+ client . _isAsyncClipboardAvailable = async ( ) => false ;
3471
3509
const expectedStr = 'cheese!' ;
3472
3510
const data = [ 3 , 0 , 0 , 0 ] ;
3473
3511
push32 ( data , expectedStr . length ) ;
3474
3512
for ( let i = 0 ; i < expectedStr . length ; i ++ ) { data . push ( expectedStr . charCodeAt ( i ) ) ; }
3475
- const spy = sinon . spy ( ) ;
3476
- client . addEventListener ( "clipboard" , spy ) ;
3513
+
3514
+ const spyPromise = new Promise ( ( resolve ) => {
3515
+ const handler = ( e ) => {
3516
+ client . removeEventListener ( "clipboard" , handler ) ;
3517
+ resolve ( e ) ;
3518
+ } ;
3519
+ client . addEventListener ( "clipboard" , handler ) ;
3520
+ } ) ;
3477
3521
3478
3522
client . _sock . _websocket . _receiveData ( new Uint8Array ( data ) ) ;
3479
- expect ( spy ) . to . have . been . calledOnce ;
3480
- expect ( spy . args [ 0 ] [ 0 ] . detail . text ) . to . equal ( expectedStr ) ;
3523
+ const event = await spyPromise ;
3524
+ expect ( event . detail . text ) . to . equal ( expectedStr ) ;
3481
3525
} ) ;
3482
3526
} ) ;
3483
3527
@@ -3531,7 +3575,8 @@ describe('Remote Frame Buffer protocol client', function () {
3531
3575
} ) ;
3532
3576
3533
3577
describe ( 'Handle Provide' , function ( ) {
3534
- it ( 'should update clipboard with correct Unicode data from a Provide message' , function ( ) {
3578
+ it ( 'should update clipboard with correct Unicode data from a Provide message' , async function ( ) {
3579
+ client . _isAsyncClipboardAvailable = async ( ) => false ;
3535
3580
let expectedData = "Aå漢字!" ;
3536
3581
let data = [ 3 , 0 , 0 , 0 ] ;
3537
3582
const flags = [ 0x10 , 0x00 , 0x00 , 0x01 ] ;
@@ -3545,16 +3590,21 @@ describe('Remote Frame Buffer protocol client', function () {
3545
3590
data = data . concat ( flags ) ;
3546
3591
data = data . concat ( Array . from ( deflatedText ) ) ;
3547
3592
3548
- const spy = sinon . spy ( ) ;
3549
- client . addEventListener ( "clipboard" , spy ) ;
3593
+ const spyPromise = new Promise ( ( resolve ) => {
3594
+ const handler = ( e ) => {
3595
+ client . removeEventListener ( "clipboard" , handler ) ;
3596
+ resolve ( e ) ;
3597
+ } ;
3598
+ client . addEventListener ( "clipboard" , handler ) ;
3599
+ } ) ;
3550
3600
3551
3601
client . _sock . _websocket . _receiveData ( new Uint8Array ( data ) ) ;
3552
- expect ( spy ) . to . have . been . calledOnce ;
3553
- expect ( spy . args [ 0 ] [ 0 ] . detail . text ) . to . equal ( expectedData ) ;
3554
- client . removeEventListener ( "clipboard" , spy ) ;
3602
+ const event = await spyPromise ;
3603
+ expect ( event . detail . text ) . to . equal ( expectedData ) ;
3555
3604
} ) ;
3556
3605
3557
- it ( 'should update clipboard with correct escape characters from a Provide message ' , function ( ) {
3606
+ it ( 'should update clipboard with correct escape characters from a Provide message ' , async function ( ) {
3607
+ client . _isAsyncClipboardAvailable = async ( ) => false ;
3558
3608
let expectedData = "Oh\nmy\n!" ;
3559
3609
let data = [ 3 , 0 , 0 , 0 ] ;
3560
3610
const flags = [ 0x10 , 0x00 , 0x00 , 0x01 ] ;
@@ -3569,16 +3619,21 @@ describe('Remote Frame Buffer protocol client', function () {
3569
3619
data = data . concat ( flags ) ;
3570
3620
data = data . concat ( Array . from ( deflatedText ) ) ;
3571
3621
3572
- const spy = sinon . spy ( ) ;
3573
- client . addEventListener ( "clipboard" , spy ) ;
3622
+ const spyPromise = new Promise ( ( resolve ) => {
3623
+ const handler = ( e ) => {
3624
+ client . removeEventListener ( "clipboard" , handler ) ;
3625
+ resolve ( e ) ;
3626
+ } ;
3627
+ client . addEventListener ( "clipboard" , handler ) ;
3628
+ } ) ;
3574
3629
3575
3630
client . _sock . _websocket . _receiveData ( new Uint8Array ( data ) ) ;
3576
- expect ( spy ) . to . have . been . calledOnce ;
3577
- expect ( spy . args [ 0 ] [ 0 ] . detail . text ) . to . equal ( expectedData ) ;
3578
- client . removeEventListener ( "clipboard" , spy ) ;
3631
+ const event = await spyPromise ;
3632
+ expect ( event . detail . text ) . to . equal ( expectedData ) ;
3579
3633
} ) ;
3580
3634
3581
- it ( 'should be able to handle large Provide messages' , function ( ) {
3635
+ it ( 'should be able to handle large Provide messages' , async function ( ) {
3636
+ client . _isAsyncClipboardAvailable = async ( ) => false ;
3582
3637
let expectedData = "hello" . repeat ( 100000 ) ;
3583
3638
let data = [ 3 , 0 , 0 , 0 ] ;
3584
3639
const flags = [ 0x10 , 0x00 , 0x00 , 0x01 ] ;
@@ -3593,13 +3648,17 @@ describe('Remote Frame Buffer protocol client', function () {
3593
3648
data = data . concat ( flags ) ;
3594
3649
data = data . concat ( Array . from ( deflatedText ) ) ;
3595
3650
3596
- const spy = sinon . spy ( ) ;
3597
- client . addEventListener ( "clipboard" , spy ) ;
3651
+ const spyPromise = new Promise ( ( resolve ) => {
3652
+ const handler = ( e ) => {
3653
+ client . removeEventListener ( "clipboard" , handler ) ;
3654
+ resolve ( e ) ;
3655
+ } ;
3656
+ client . addEventListener ( "clipboard" , handler ) ;
3657
+ } ) ;
3598
3658
3599
3659
client . _sock . _websocket . _receiveData ( new Uint8Array ( data ) ) ;
3600
- expect ( spy ) . to . have . been . calledOnce ;
3601
- expect ( spy . args [ 0 ] [ 0 ] . detail . text ) . to . equal ( expectedData ) ;
3602
- client . removeEventListener ( "clipboard" , spy ) ;
3660
+ const event = await spyPromise ;
3661
+ expect ( event . detail . text ) . to . equal ( expectedData ) ;
3603
3662
} ) ;
3604
3663
3605
3664
} ) ;
0 commit comments