|
| 1 | +using DCL.AvatarRendering.AvatarShape.UnityInterface; |
| 2 | +using DCL.AvatarRendering.Emotes.Play; |
| 3 | +using NSubstitute; |
| 4 | +using NUnit.Framework; |
| 5 | +using UnityEngine; |
| 6 | +using Object = UnityEngine.Object; |
| 7 | + |
| 8 | +namespace DCL.AvatarRendering.Emotes.Tests |
| 9 | +{ |
| 10 | + public class EmotePlayerShould |
| 11 | + { |
| 12 | + private GameObject poolRoot = null!; |
| 13 | + private GameObject audioSourcePrefab = null!; |
| 14 | + private GameObject legacyEmoteAsset = null!; |
| 15 | + private GameObject avatarGameObject = null!; |
| 16 | + private EmotePlayer emotePlayer = null!; |
| 17 | + private IAvatarView avatarView = null!; |
| 18 | + |
| 19 | + [SetUp] |
| 20 | + public void Setup() |
| 21 | + { |
| 22 | + poolRoot = new GameObject("ROOT_POOL_CONTAINER"); |
| 23 | + audioSourcePrefab = new GameObject("EmoteAudioSource"); |
| 24 | + audioSourcePrefab.AddComponent<AudioSource>(); |
| 25 | + |
| 26 | + emotePlayer = new EmotePlayer(audioSourcePrefab.GetComponent<AudioSource>(), |
| 27 | + ScriptableObject.CreateInstance<EmoteMaskCatalog>(), legacyAnimationsEnabled: false); |
| 28 | + |
| 29 | + // Legacy emote asset: an Animator with no controller (so CreateNewEmoteReference takes the |
| 30 | + // legacy branch) plus an Animation carrying a legacy clip. |
| 31 | + legacyEmoteAsset = new GameObject("LegacyEmoteAsset"); |
| 32 | + legacyEmoteAsset.AddComponent<Animator>(); |
| 33 | + var animation = legacyEmoteAsset.AddComponent<Animation>(); |
| 34 | + var clip = new AnimationClip { legacy = true, name = "wave" }; |
| 35 | + animation.AddClip(clip, clip.name); |
| 36 | + |
| 37 | + avatarGameObject = new GameObject("Avatar"); |
| 38 | + |
| 39 | + avatarView = Substitute.For<IAvatarView>(); |
| 40 | + avatarView.GetTransform().Returns(avatarGameObject.transform); |
| 41 | + } |
| 42 | + |
| 43 | + [TearDown] |
| 44 | + public void TearDown() |
| 45 | + { |
| 46 | + Object.DestroyImmediate(avatarGameObject); |
| 47 | + Object.DestroyImmediate(legacyEmoteAsset); |
| 48 | + Object.DestroyImmediate(audioSourcePrefab); |
| 49 | + Object.DestroyImmediate(poolRoot); |
| 50 | + } |
| 51 | + |
| 52 | + // Regression for https://github.com/decentraland/unity-explorer/issues/9665: a legacy emote |
| 53 | + // rejected because legacyAnimationsEnabled is false must return its pooled instance to the pool. |
| 54 | + // Before the fix the reference was registered in emotesInUse only after the early-out, so Stop() |
| 55 | + // released nothing and the instance stayed parented under the avatar, leaking on every play. |
| 56 | + [Test] |
| 57 | + public void NotLeakPooledInstanceWhenLegacyEmoteRejected() |
| 58 | + { |
| 59 | + var emoteComponent = new CharacterEmoteComponent(); |
| 60 | + |
| 61 | + bool played = emotePlayer.Play(legacyEmoteAsset, null, false, false, in avatarView, ref emoteComponent); |
| 62 | + |
| 63 | + Assert.IsFalse(played); |
| 64 | + Assert.IsNull(emoteComponent.CurrentEmoteReference); |
| 65 | + Assert.IsNull(avatarGameObject.GetComponentInChildren<EmoteReferences>(true), |
| 66 | + "Rejected legacy emote leaked a pooled instance under the avatar."); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments