@@ -390,6 +390,162 @@ define(function (require, exports, module) {
390390 } ) ;
391391 } ) ;
392392
393+ describe ( "Screenshot Capture API Tests" , function ( ) {
394+ const PNG_SIGNATURE = [ 137 , 80 , 78 , 71 , 13 , 10 , 26 , 10 ] ; // PNG magic bytes
395+
396+ function isPNG ( bytes ) {
397+ if ( bytes . length < 8 ) {
398+ return false ;
399+ }
400+ for ( let i = 0 ; i < PNG_SIGNATURE . length ; i ++ ) {
401+ if ( bytes [ i ] !== PNG_SIGNATURE [ i ] ) {
402+ return false ;
403+ }
404+ }
405+ return true ;
406+ }
407+
408+ describe ( "screenShotBinary" , function ( ) {
409+ it ( "Should return a Uint8Array of PNG data for full page capture" , async function ( ) {
410+ const bytes = await Phoenix . app . screenShotBinary ( ) ;
411+ expect ( bytes instanceof Uint8Array ) . toBeTrue ( ) ;
412+ expect ( bytes . length ) . toBeGreaterThan ( 0 ) ;
413+ expect ( isPNG ( bytes ) ) . withContext ( "Result should be valid PNG data" ) . toBeTrue ( ) ;
414+ } ) ;
415+
416+ it ( "Should return a Uint8Array of PNG data for bounded capture" , async function ( ) {
417+ const bytes = await Phoenix . app . screenShotBinary ( { x : 0 , y : 0 , width : 100 , height : 100 } ) ;
418+ expect ( bytes instanceof Uint8Array ) . toBeTrue ( ) ;
419+ expect ( bytes . length ) . toBeGreaterThan ( 0 ) ;
420+ expect ( isPNG ( bytes ) ) . withContext ( "Result should be valid PNG data" ) . toBeTrue ( ) ;
421+ } ) ;
422+
423+ it ( "Should throw when rect is missing required fields" , async function ( ) {
424+ await expectAsync (
425+ Phoenix . app . screenShotBinary ( { x : 0 , y : 0 } )
426+ ) . toBeRejectedWithError ( "rect must include all fields: x, y, width, height" ) ;
427+ } ) ;
428+
429+ it ( "Should throw when rect fields are not numbers" , async function ( ) {
430+ await expectAsync (
431+ Phoenix . app . screenShotBinary ( { x : "0" , y : 0 , width : 100 , height : 100 } )
432+ ) . toBeRejectedWithError ( "rect fields x, y, width, height must be numbers" ) ;
433+ } ) ;
434+
435+ it ( "Should throw when rect fields are negative" , async function ( ) {
436+ await expectAsync (
437+ Phoenix . app . screenShotBinary ( { x : - 1 , y : 0 , width : 100 , height : 100 } )
438+ ) . toBeRejectedWithError ( "rect fields x, y, width, height must be non-negative" ) ;
439+ } ) ;
440+
441+ it ( "Should throw when rect width is 0" , async function ( ) {
442+ await expectAsync (
443+ Phoenix . app . screenShotBinary ( { x : 0 , y : 0 , width : 0 , height : 100 } )
444+ ) . toBeRejectedWithError ( "rect width and height must be greater than 0" ) ;
445+ } ) ;
446+
447+ it ( "Should throw when rect height is 0" , async function ( ) {
448+ await expectAsync (
449+ Phoenix . app . screenShotBinary ( { x : 0 , y : 0 , width : 100 , height : 0 } )
450+ ) . toBeRejectedWithError ( "rect width and height must be greater than 0" ) ;
451+ } ) ;
452+
453+ it ( "Should throw when rect exceeds window width bounds" , async function ( ) {
454+ await expectAsync (
455+ Phoenix . app . screenShotBinary ( { x : 0 , y : 0 , width : 999999 , height : 100 } )
456+ ) . toBeRejectedWithError ( "rect x + width exceeds window innerWidth" ) ;
457+ } ) ;
458+
459+ it ( "Should throw when rect exceeds window height bounds" , async function ( ) {
460+ await expectAsync (
461+ Phoenix . app . screenShotBinary ( { x : 0 , y : 0 , width : 100 , height : 999999 } )
462+ ) . toBeRejectedWithError ( "rect y + height exceeds window innerHeight" ) ;
463+ } ) ;
464+ } ) ;
465+
466+ describe ( "screenShotToBlob" , function ( ) {
467+ it ( "Should return a Blob of type image/png for full page capture" , async function ( ) {
468+ const blob = await Phoenix . app . screenShotToBlob ( ) ;
469+ expect ( blob instanceof Blob ) . toBeTrue ( ) ;
470+ expect ( blob . type ) . toEqual ( "image/png" ) ;
471+ expect ( blob . size ) . toBeGreaterThan ( 0 ) ;
472+ } ) ;
473+
474+ it ( "Should return a Blob of type image/png for bounded capture" , async function ( ) {
475+ const blob = await Phoenix . app . screenShotToBlob ( { x : 0 , y : 0 , width : 100 , height : 100 } ) ;
476+ expect ( blob instanceof Blob ) . toBeTrue ( ) ;
477+ expect ( blob . type ) . toEqual ( "image/png" ) ;
478+ expect ( blob . size ) . toBeGreaterThan ( 0 ) ;
479+ } ) ;
480+ } ) ;
481+
482+ describe ( "screenShotToPNGFile" , function ( ) {
483+ let testDir ;
484+ let testFilePath ;
485+ const testFileName = "screenshot-test-output.png" ;
486+
487+ beforeEach ( async function ( ) {
488+ const appLocalData = fs . getTauriVirtualPath ( await platform . appLocalDataDir ( ) ) ;
489+ testDir = appLocalData ;
490+ testFilePath = `${ testDir } /${ testFileName } ` ;
491+ } ) ;
492+
493+ afterEach ( async function ( ) {
494+ // Always clean up the test file, even if the test failed
495+ await SpecRunnerUtils . deletePathAsync ( testFilePath ) . catch ( ( ) => { } ) ;
496+ } ) ;
497+
498+ it ( "Should write a valid PNG file" , async function ( ) {
499+ await Phoenix . app . screenShotToPNGFile ( testFilePath ) ;
500+ // Read back and verify PNG signature
501+ const content = await new Promise ( ( resolve , reject ) => {
502+ fs . readFile ( testFilePath , 'binary' , ( err , data ) => {
503+ if ( err ) {
504+ reject ( err ) ;
505+ } else {
506+ resolve ( data ) ;
507+ }
508+ } ) ;
509+ } ) ;
510+ const bytes = new Uint8Array ( content ) ;
511+ expect ( isPNG ( bytes ) ) . withContext ( "Written file should be valid PNG" ) . toBeTrue ( ) ;
512+ } ) ;
513+
514+ it ( "Should write a valid PNG file with bounded rect" , async function ( ) {
515+ await Phoenix . app . screenShotToPNGFile ( testFilePath , { x : 0 , y : 0 , width : 100 , height : 100 } ) ;
516+ const content = await new Promise ( ( resolve , reject ) => {
517+ fs . readFile ( testFilePath , 'binary' , ( err , data ) => {
518+ if ( err ) {
519+ reject ( err ) ;
520+ } else {
521+ resolve ( data ) ;
522+ }
523+ } ) ;
524+ } ) ;
525+ const bytes = new Uint8Array ( content ) ;
526+ expect ( isPNG ( bytes ) ) . withContext ( "Written file should be valid PNG" ) . toBeTrue ( ) ;
527+ } ) ;
528+
529+ it ( "Should throw when filePathToSave is not provided" , async function ( ) {
530+ await expectAsync (
531+ Phoenix . app . screenShotToPNGFile ( )
532+ ) . toBeRejectedWithError ( "filePathToSave must be a non-empty string" ) ;
533+ } ) ;
534+
535+ it ( "Should throw when filePathToSave is not a string" , async function ( ) {
536+ await expectAsync (
537+ Phoenix . app . screenShotToPNGFile ( 123 )
538+ ) . toBeRejectedWithError ( "filePathToSave must be a non-empty string" ) ;
539+ } ) ;
540+
541+ it ( "Should throw when filePathToSave is an empty string" , async function ( ) {
542+ await expectAsync (
543+ Phoenix . app . screenShotToPNGFile ( "" )
544+ ) . toBeRejectedWithError ( "filePathToSave must be a non-empty string" ) ;
545+ } ) ;
546+ } ) ;
547+ } ) ;
548+
393549 describe ( "Credentials OTP API Tests" , function ( ) {
394550 const scopeName = "testScope" ;
395551 const trustRing = window . specRunnerTestKernalModeTrust ;
0 commit comments