Skip to content

Truly independent web browser

Tests/LibWeb: Add a test for collecting removed lazy images

Andreas Kling3d94f06ac1e0
diff --git a/Tests/LibWeb/Text/expected/HTML/removed-lazy-image-is-collectable.txt b/Tests/LibWeb/Text/expected/HTML/removed-lazy-image-is-collectable.txt
new file mode 100644
index 00000000000..822383c3de2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/removed-lazy-image-is-collectable.txt
@@ -0,0 +1 @@
+collected 0 of 5 removed lazy images
diff --git a/Tests/LibWeb/Text/input/HTML/removed-lazy-image-is-collectable.html b/Tests/LibWeb/Text/input/HTML/removed-lazy-image-is-collectable.html
new file mode 100644
index 00000000000..ea27bab7f88
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/removed-lazy-image-is-collectable.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<div id="container" style="margin-top: 10000px"></div>
+<script>
+ // A lazy image that was removed before it came into view never fetches, so nothing may keep it alive: its shared
+ // request with the other images of the same URL stays unfetched, and must not hold the callbacks of the image.
+ promiseTest(async () => {
+ const weakImages = [];
+ (() => {
+ for (let i = 0; i < 5; i++) {
+ const image = document.createElement("img");
+ image.loading = "lazy";
+ image.src = "../../../Assets/120.png";
+ // NB: An expando preserves the wrapper, so that the WeakRef follows the image rather than its wrapper.
+ image.preserved = true;
+ container.appendChild(image);
+ weakImages.push(new WeakRef(image));
+ }
+ })();
+ // Let the images update their image data, which defers their fetches until they come into view.
+ await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+ container.replaceChildren();
+
+ await collectGarbageUntilCleared(weakImages);
+
+ const collected = weakImages.filter(weak => weak.deref() === undefined).length;
+ println(`collected ${collected} of ${weakImages.length} removed lazy images`);
+ });
+</script>