test_image_cache_notification.html (1392B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <script src="/tests/SimpleTest/SimpleTest.js"></script> 5 </head> 6 <body> 7 <button>Add Image</button> 8 <script> 9 /* Test to ensure http-on-resource-cache-response should only be notified 10 * once per image 11 */ 12 13 SimpleTest.waitForExplicitFinish(); 14 async function addImage() { 15 const newImage = document.createElement("img"); 16 17 const imageLoaded = new Promise((r) => { 18 newImage.onload = r; 19 }); 20 21 newImage.src = "./over.png"; 22 document.body.appendChild(newImage); 23 return imageLoaded; 24 } 25 26 let imageCacheCallbackRunCount = 0; 27 const cb = SpecialPowers.wrapCallback(() => { 28 imageCacheCallbackRunCount += 1; 29 }); 30 SpecialPowers.addObserver(cb, "http-on-resource-cache-response"); 31 32 async function runTest() { 33 await addImage(); 34 SimpleTest.ok(imageCacheCallbackRunCount == 0, "first load of over.png shouldn't be cached"); 35 await addImage(); 36 SimpleTest.ok(imageCacheCallbackRunCount == 1, "second load of over.png should be cached"); 37 await addImage(); 38 await addImage(); 39 await addImage(); 40 SimpleTest.ok(imageCacheCallbackRunCount == 1, "further loads of over.png shouldn't be notified"); 41 SimpleTest.finish(); 42 } 43 44 runTest(); 45 </script> 46 </body> 47 </html>