From 1a7e100cb9ef8fd1d2886f0a8a9b3cd067757982 Mon Sep 17 00:00:00 2001 From: Vincent Fretin Date: Tue, 16 Dec 2025 13:05:24 +0100 Subject: [PATCH 1/2] Use THREE.Cache methods instead of accessing directly THREE.Cache.files in tests --- tests/components/material.test.js | 6 +++--- tests/components/sound.test.js | 2 +- tests/core/a-assets.test.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/components/material.test.js b/tests/components/material.test.js index 33d616c8006..ab0188f717b 100644 --- a/tests/components/material.test.js +++ b/tests/components/material.test.js @@ -176,12 +176,12 @@ suite('material', function () { THREE.Cache.clear(); assetsEl.appendChild(img); el.sceneEl.appendChild(assetsEl); - // Adding the asset will add image:${IMG_SRC} in THREE.Cache.files + // Adding the asset will add image:${IMG_SRC} in THREE.Cache // without going through THREE.ImageLoader el.addEventListener('materialtextureloaded', function () { assert.notOk(imageLoaderSpy.called); assert.notOk(textureLoaderSpy.called); - assert.ok(`image:${IMG_SRC}` in THREE.Cache.files); + assert.equal(THREE.Cache.get(`image:${IMG_SRC}`), img); THREE.Cache.clear(); THREE.ImageLoader.prototype.load.restore(); THREE.TextureLoader.prototype.load.restore(); @@ -194,7 +194,7 @@ suite('material', function () { var imageLoaderSpy = this.sinon.spy(THREE.ImageLoader.prototype, 'load'); el.addEventListener('materialtextureloaded', function () { assert.ok(imageLoaderSpy.called); - assert.ok(`image:${IMG_SRC}` in THREE.Cache.files); + assert.ok(!!THREE.Cache.get(`image:${IMG_SRC}`)); THREE.ImageLoader.prototype.load.restore(); done(); }); diff --git a/tests/components/sound.test.js b/tests/components/sound.test.js index 399afd42b04..c0e2b048913 100644 --- a/tests/components/sound.test.js +++ b/tests/components/sound.test.js @@ -5,7 +5,7 @@ import THREE from 'lib/three.js'; suite('sound', function () { setup(function (done) { var el = this.el = entityFactory(); - THREE.Cache.files = {}; + THREE.Cache.clear(); setTimeout(() => { el.setAttribute('sound', { autoplay: true, diff --git a/tests/core/a-assets.test.js b/tests/core/a-assets.test.js index 24528444b72..b3f11b23cc0 100644 --- a/tests/core/a-assets.test.js +++ b/tests/core/a-assets.test.js @@ -20,7 +20,7 @@ suite('a-assets', function () { done(); }); document.body.appendChild(scene); - THREE.Cache.files = {}; + THREE.Cache.clear(); }); test('loads even if one asset fails to load', function (done) { @@ -81,7 +81,7 @@ suite('a-assets', function () { assetsEl.appendChild(img); img.addEventListener('load', function () { - assert.equal(THREE.Cache.files[`image:${IMG_SRC}`], img); + assert.equal(THREE.Cache.get(`image:${IMG_SRC}`), img); done(); }); From aa4db04334b8818160696de247f7ff4bfd4f4dba Mon Sep 17 00:00:00 2001 From: Vincent Fretin Date: Wed, 17 Dec 2025 10:26:07 +0100 Subject: [PATCH 2/2] Set the image cache when img is loaded --- src/core/a-assets.js | 7 +++++-- tests/components/material.test.js | 14 +++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/core/a-assets.js b/src/core/a-assets.js index 89a38a1f082..ad14a0ecdbe 100644 --- a/src/core/a-assets.js +++ b/src/core/a-assets.js @@ -41,12 +41,15 @@ class AAssets extends ANode { loaded.push(new Promise(function (resolve, reject) { // Set in cache because we won't be needing to call three.js loader if we have. // a loaded media element. - THREE.Cache.add('image:' + imgEls[i].getAttribute('src'), imgEl); if (imgEl.complete) { + THREE.Cache.add('image:' + imgEls[i].getAttribute('src'), imgEl); resolve(); return; } - imgEl.onload = resolve; + imgEl.onload = function () { + THREE.Cache.add('image:' + imgEls[i].getAttribute('src'), imgEl); + resolve(); + }; imgEl.onerror = reject; })); } diff --git a/tests/components/material.test.js b/tests/components/material.test.js index ab0188f717b..d004d4f9fb8 100644 --- a/tests/components/material.test.js +++ b/tests/components/material.test.js @@ -176,16 +176,20 @@ suite('material', function () { THREE.Cache.clear(); assetsEl.appendChild(img); el.sceneEl.appendChild(assetsEl); - // Adding the asset will add image:${IMG_SRC} in THREE.Cache - // without going through THREE.ImageLoader + // Adding the asset will add image:${IMG_SRC} in THREE.Cache when the img + // loading is complete with img.onload, without going through THREE.ImageLoader + assert.notOk(!!THREE.Cache.get(`image:${IMG_SRC}`)); el.addEventListener('materialtextureloaded', function () { assert.notOk(imageLoaderSpy.called); assert.notOk(textureLoaderSpy.called); - assert.equal(THREE.Cache.get(`image:${IMG_SRC}`), img); - THREE.Cache.clear(); THREE.ImageLoader.prototype.load.restore(); THREE.TextureLoader.prototype.load.restore(); - done(); + // load event is triggered after this materialtextureloaded callback + img.addEventListener('load', function () { + assert.equal(THREE.Cache.get(`image:${IMG_SRC}`), img); + THREE.Cache.clear(); + done(); + }, {once: true}); }); el.setAttribute('material', 'src', '#foo'); });