@@ -166,15 +166,24 @@ public void ImageRenders_WithCorrectDimensions()
166166    [ Fact ] 
167167    public  void  Image_CompletesLoad_AfterArtificialDelay ( ) 
168168    { 
169-         // Patch  setContentAsync to introduce a delay before delegating to original  
169+         // Instrument  setContentAsync to pause before fulfilling first image load until explicitly resolved.  
170170        ( ( IJavaScriptExecutor ) Browser ) . ExecuteScript ( @" 
171171            (function(){ 
172172              const root = Blazor && Blazor._internal && Blazor._internal.BinaryMedia; 
173173              if (!root) return; 
174174              if (!window.__origSetContentAsync) { 
175175                window.__origSetContentAsync = root.setContentAsync; 
176176                root.setContentAsync = async function(...args){ 
177-                   await new Promise(r => setTimeout(r, 500)); 
177+                   const getResolvers = () => { 
178+                     if (Promise.fromResolvers) return Promise.fromResolvers(); 
179+                     let resolve, reject; const promise = new Promise((r,j)=>{ resolve=r; reject=j; }); 
180+                     return { promise, resolve, reject }; 
181+                   }; 
182+                   if (!window.__imageContentDelay){ 
183+                     const resolvers = getResolvers(); 
184+                     window.__imageContentDelay = resolvers; // first invocation delayed 
185+                     await resolvers.promise; 
186+                   } 
178187                  return window.__origSetContentAsync.apply(this, args); 
179188                }; 
180189              } 
@@ -183,21 +192,26 @@ public void Image_CompletesLoad_AfterArtificialDelay()
183192        Browser . FindElement ( By . Id ( "load-png" ) ) . Click ( ) ; 
184193
185194        var  imageElement  =  Browser . FindElement ( By . Id ( "png-basic" ) ) ; 
186-         Browser . True ( ( )  => 
187-         { 
195+         Assert . NotNull ( imageElement ) ; 
196+ 
197+         // Release the delayed promise so load can complete. 
198+         ( ( IJavaScriptExecutor ) Browser ) . ExecuteScript ( "if (window.__imageContentDelay) { window.__imageContentDelay.resolve(); }" ) ; 
199+ 
200+         Browser . True ( ( )  =>  { 
188201            var  src  =  imageElement . GetAttribute ( "src" ) ; 
189202            return  ! string . IsNullOrEmpty ( src )  &&  src . StartsWith ( "blob:" ,  StringComparison . Ordinal ) ; 
190203        } ) ; 
191204        Browser . Equal ( "PNG basic loaded" ,  ( )  =>  Browser . FindElement ( By . Id ( "current-status" ) ) . Text ) ; 
192205
193-         // Restore 
206+         // Restore original function and clean up instrumentation  
194207        ( ( IJavaScriptExecutor ) Browser ) . ExecuteScript ( @" 
195208            (function(){ 
196209              const root = Blazor && Blazor._internal && Blazor._internal.BinaryMedia; 
197210              if (root && window.__origSetContentAsync) { 
198211                root.setContentAsync = window.__origSetContentAsync; 
199212                delete window.__origSetContentAsync; 
200213              } 
214+               delete window.__imageContentDelay; 
201215            })();" ) ; 
202216    } 
203217
@@ -341,4 +355,19 @@ public void UrlRevoked_WhenImageRemovedFromDom()
341355            } 
342356        } ) ; 
343357    } 
358+ 
359+     [ Fact ] 
360+     public  void  InvalidMimeImage_SetsErrorState ( ) 
361+     { 
362+         Browser . FindElement ( By . Id ( "load-invalid-mime" ) ) . Click ( ) ; 
363+         Browser . Equal ( "Invalid mime image loaded" ,  ( )  =>  Browser . FindElement ( By . Id ( "current-status" ) ) . Text ) ; 
364+ 
365+         var  img  =  Browser . FindElement ( By . Id ( "invalid-mime-image" ) ) ; 
366+         Assert . NotNull ( img ) ; 
367+ 
368+         Browser . Equal ( "error" ,  ( )  =>  img . GetAttribute ( "data-state" ) ) ; 
369+ 
370+         var  src  =  img . GetAttribute ( "src" ) ; 
371+         Assert . True ( string . IsNullOrEmpty ( src )  ||  src . StartsWith ( "blob:" ,  StringComparison . Ordinal ) ) ; 
372+     } 
344373} 
0 commit comments