| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using NUnit.Framework; |
| | 6 | | using System.Collections; |
| | 7 | | using System.Linq; |
| | 8 | | using Newtonsoft.Json; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.TestTools; |
| | 11 | |
|
| | 12 | | namespace Tests |
| | 13 | | { |
| | 14 | | public class AudioTests : IntegrationTestSuite_Legacy |
| | 15 | | { |
| | 16 | | protected override IEnumerator TearDown() |
| | 17 | | { |
| 15 | 18 | | sceneController.enabled = true; |
| 15 | 19 | | return base.TearDown(); |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public DCLAudioClip CreateAudioClip(string url, bool loop, bool shouldTryToLoad, double volume) |
| | 23 | | { |
| 3 | 24 | | DCLAudioClip.Model model = new DCLAudioClip.Model |
| | 25 | | { |
| | 26 | | url = url, |
| | 27 | | loop = loop, |
| | 28 | | shouldTryToLoad = shouldTryToLoad, |
| | 29 | | volume = volume |
| | 30 | | }; |
| | 31 | |
|
| 3 | 32 | | return CreateAudioClip(model); |
| | 33 | | } |
| | 34 | |
|
| 4 | 35 | | public DCLAudioClip CreateAudioClip(DCLAudioClip.Model model) { return TestHelpers.SharedComponentCreate<DCLAudi |
| | 36 | |
|
| | 37 | | public IEnumerator CreateAndLoadAudioClip(bool waitForLoading = true) |
| | 38 | | { |
| 2 | 39 | | var entity = TestHelpers.CreateSceneEntity(scene); |
| 2 | 40 | | yield return null; |
| | 41 | |
|
| 2 | 42 | | yield return TestHelpers.CreateAudioSourceWithClipForEntity(entity); |
| | 43 | |
|
| 2 | 44 | | DCLAudioSource dclAudioSource = entity.gameObject.GetComponentInChildren<DCLAudioSource>(); |
| 2 | 45 | | AudioSource unityAudioSource = dclAudioSource.GetComponentInChildren<AudioSource>(); |
| | 46 | |
|
| 2 | 47 | | Assert.IsTrue(scene.entities.ContainsKey(entity.entityId), "Entity was not created correctly!"); |
| 2 | 48 | | Assert.IsTrue(dclAudioSource != null, "DCLAudioSource Creation Failure!"); |
| 2 | 49 | | Assert.IsTrue(unityAudioSource != null, "Unity AudioSource Creation Failure!"); |
| | 50 | |
|
| 2 | 51 | | yield return dclAudioSource.routine; |
| | 52 | |
|
| 2 | 53 | | Assert.IsTrue(unityAudioSource.isPlaying, "Audio Source is not playing when it should!"); |
| | 54 | |
|
| | 55 | | //NOTE(Brian): Stop test |
| 2 | 56 | | yield return TestHelpers.CreateAudioSource(scene, |
| | 57 | | entityId: entity.entityId, |
| | 58 | | audioClipId: "audioClipTest", |
| | 59 | | playing: false); |
| | 60 | |
|
| 2 | 61 | | yield return null; |
| | 62 | |
|
| 2 | 63 | | Assert.IsTrue(!unityAudioSource.isPlaying, "Audio Source is playing when it should NOT play!"); |
| 2 | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// This should test creating a audioclip/audiosource couple, wait for audioClip load and send playing:true afte |
| | 68 | | /// </summary> |
| | 69 | | [UnityTest] |
| 2 | 70 | | public IEnumerator CreateAndLoadAudioClipTest() { yield return CreateAndLoadAudioClip(waitForLoading: true); } |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// This should test creating a audioclip/audiosource couple but send playing:true before the audioClip finished |
| | 74 | | /// </summary> |
| | 75 | | [UnityTest] |
| 2 | 76 | | public IEnumerator PlayAudioTestWithoutFinishLoading() { yield return CreateAndLoadAudioClip(waitForLoading: fal |
| | 77 | |
|
| | 78 | | [UnityTest] |
| 2 | 79 | | public IEnumerator AudioComponentMissingValuesGetDefaultedOnUpdate() { yield return TestHelpers.TestEntityCompon |
| | 80 | |
|
| | 81 | | [UnityTest] |
| | 82 | | public IEnumerator AudioClipMissingValuesGetDefaultedOnUpdate() |
| | 83 | | { |
| | 84 | | // 1. Create component with non-default configs |
| 1 | 85 | | DCLAudioClip.Model componentModel = new DCLAudioClip.Model |
| | 86 | | { |
| | 87 | | loop = true, |
| | 88 | | shouldTryToLoad = false, |
| | 89 | | volume = 0.8f |
| | 90 | | }; |
| | 91 | |
|
| 1 | 92 | | DCLAudioClip audioClip = CreateAudioClip(componentModel); |
| | 93 | |
|
| 1 | 94 | | yield return audioClip.routine; |
| | 95 | |
|
| | 96 | | // 2. Check configured values |
| 1 | 97 | | Assert.IsTrue(audioClip.isLoop); |
| 1 | 98 | | Assert.IsFalse(audioClip.shouldTryLoad); |
| 1 | 99 | | Assert.AreEqual(0.8f, audioClip.volume); |
| | 100 | |
|
| | 101 | | // 3. Update component with missing values |
| 1 | 102 | | componentModel = new DCLAudioClip.Model { }; |
| | 103 | |
|
| 1 | 104 | | scene.SharedComponentUpdate(audioClip.id, JsonUtility.ToJson(componentModel)); |
| | 105 | |
|
| 1 | 106 | | yield return audioClip.routine; |
| | 107 | |
|
| | 108 | | // 4. Check defaulted values |
| 1 | 109 | | Assert.IsFalse(audioClip.isLoop); |
| 1 | 110 | | Assert.IsTrue(audioClip.shouldTryLoad); |
| 1 | 111 | | Assert.AreEqual(1f, audioClip.volume); |
| 1 | 112 | | } |
| | 113 | |
|
| | 114 | | [UnityTest] |
| | 115 | | public IEnumerator AudioIsLooped() |
| | 116 | | { |
| 1 | 117 | | var entity = TestHelpers.CreateSceneEntity(scene); |
| 1 | 118 | | yield return null; |
| | 119 | |
|
| 1 | 120 | | yield return TestHelpers.LoadAudioClip(scene, "1", TestAssetsUtils.GetPath() + "/Audio/short_effect.ogg", fa |
| | 121 | |
|
| 1 | 122 | | yield return TestHelpers.CreateAudioSource(scene, entity.entityId, "1", true, loop: true); |
| | 123 | |
|
| 2 | 124 | | DCLAudioSource dclAudioSource = entity.components.Values.FirstOrDefault(x => x is DCLAudioSource) as DCLAudi |
| 1 | 125 | | float initTime = dclAudioSource.audioSource.clip.length - 0.05f; |
| 1 | 126 | | dclAudioSource.audioSource.time = initTime; |
| 1 | 127 | | yield return new WaitForSeconds(0.1f); |
| | 128 | |
|
| 1 | 129 | | Assert.IsTrue(dclAudioSource.playTime > 0 && dclAudioSource.playTime < initTime); |
| 1 | 130 | | } |
| | 131 | |
|
| | 132 | | [UnityTest] |
| | 133 | | public IEnumerator AudioIsNotLooped() |
| | 134 | | { |
| 1 | 135 | | var entity = TestHelpers.CreateSceneEntity(scene); |
| 1 | 136 | | yield return null; |
| | 137 | |
|
| 1 | 138 | | yield return TestHelpers.LoadAudioClip(scene, "1", TestAssetsUtils.GetPath() + "/Audio/short_effect.ogg", fa |
| | 139 | |
|
| 1 | 140 | | yield return TestHelpers.CreateAudioSource(scene, entity.entityId, "1", true, loop: false); |
| | 141 | |
|
| 2 | 142 | | DCLAudioSource dclAudioSource = entity.components.Values.FirstOrDefault(x => x is DCLAudioSource) as DCLAudi |
| 1 | 143 | | dclAudioSource.audioSource.time = dclAudioSource.audioSource.clip.length - 0.05f; |
| 1 | 144 | | yield return new WaitForSeconds(0.1f); |
| | 145 | |
|
| 1 | 146 | | Assert.AreEqual(0, dclAudioSource.playTime); |
| 1 | 147 | | } |
| | 148 | |
|
| | 149 | | [UnityTest] |
| | 150 | | public IEnumerator AudioClipAttachedGetsReplacedOnNewAttachment() |
| | 151 | | { |
| 1 | 152 | | yield return TestHelpers.TestAttachedSharedComponentOfSameTypeIsReplaced<DCLAudioClip.Model, DCLAudioClip>( |
| | 153 | | scene, CLASS_ID.AUDIO_CLIP); |
| 1 | 154 | | } |
| | 155 | |
|
| | 156 | | [UnityTest] |
| | 157 | | public IEnumerator VolumeWhenAudioCreatedWithNoUserInScene() |
| | 158 | | { |
| | 159 | | // We disable SceneController monobehaviour to avoid its current scene id update |
| 1 | 160 | | sceneController.enabled = false; |
| | 161 | |
|
| | 162 | | // Set current scene as a different one |
| 1 | 163 | | CommonScriptableObjects.sceneID.Set("unexistent-scene"); |
| | 164 | |
|
| 1 | 165 | | var entity = TestHelpers.CreateSceneEntity(scene); |
| 1 | 166 | | yield return null; |
| | 167 | |
|
| 1 | 168 | | yield return TestHelpers.CreateAudioSourceWithClipForEntity(entity); |
| | 169 | |
|
| 1 | 170 | | DCLAudioSource dclAudioSource = entity.gameObject.GetComponentInChildren<DCLAudioSource>(); |
| 1 | 171 | | yield return dclAudioSource.routine; |
| | 172 | |
|
| 1 | 173 | | AudioSource unityAudioSource = dclAudioSource.GetComponentInChildren<AudioSource>(); |
| | 174 | |
|
| | 175 | | // Check the volume |
| 1 | 176 | | Assert.AreEqual(0f, unityAudioSource.volume); |
| 1 | 177 | | } |
| | 178 | |
|
| | 179 | | [UnityTest] |
| | 180 | | public IEnumerator VolumeWhenAudioCreatedWithUserInScene() |
| | 181 | | { |
| | 182 | | // We disable SceneController monobehaviour to avoid its current scene id update |
| 1 | 183 | | sceneController.enabled = false; |
| | 184 | |
|
| | 185 | | // Set current scene with this scene's id |
| 1 | 186 | | CommonScriptableObjects.sceneID.Set(scene.sceneData.id); |
| | 187 | |
|
| 1 | 188 | | var entity = TestHelpers.CreateSceneEntity(scene); |
| 1 | 189 | | yield return null; |
| | 190 | |
|
| 1 | 191 | | yield return TestHelpers.CreateAudioSourceWithClipForEntity(entity); |
| | 192 | |
|
| 1 | 193 | | DCLAudioSource dclAudioSource = entity.gameObject.GetComponentInChildren<DCLAudioSource>(); |
| 1 | 194 | | yield return dclAudioSource.routine; |
| | 195 | |
|
| 1 | 196 | | AudioSource unityAudioSource = dclAudioSource.GetComponentInChildren<AudioSource>(); |
| | 197 | |
|
| | 198 | | // Check the volume |
| 1 | 199 | | Assert.AreEqual(unityAudioSource.volume, dclAudioSource.volume); |
| 1 | 200 | | } |
| | 201 | |
|
| | 202 | | [UnityTest] |
| | 203 | | public IEnumerator VolumeIsMutedWhenUserLeavesScene() |
| | 204 | | { |
| | 205 | | // We disable SceneController monobehaviour to avoid its current scene id update |
| 1 | 206 | | sceneController.enabled = false; |
| | 207 | |
|
| | 208 | | // Set current scene with this scene's id |
| 1 | 209 | | CommonScriptableObjects.sceneID.Set(scene.sceneData.id); |
| | 210 | |
|
| 1 | 211 | | var entity = TestHelpers.CreateSceneEntity(scene); |
| 1 | 212 | | yield return null; |
| | 213 | |
|
| 1 | 214 | | yield return TestHelpers.CreateAudioSourceWithClipForEntity(entity); |
| | 215 | |
|
| 1 | 216 | | DCLAudioSource dclAudioSource = entity.gameObject.GetComponentInChildren<DCLAudioSource>(); |
| 1 | 217 | | yield return dclAudioSource.routine; |
| | 218 | |
|
| 1 | 219 | | AudioSource unityAudioSource = dclAudioSource.GetComponentInChildren<AudioSource>(); |
| | 220 | |
|
| | 221 | | // Set current scene as a different one |
| 1 | 222 | | CommonScriptableObjects.sceneID.Set("unexistent-scene"); |
| | 223 | |
|
| | 224 | | // Check the volume |
| 1 | 225 | | Assert.AreEqual(unityAudioSource.volume, 0f); |
| 1 | 226 | | } |
| | 227 | |
|
| | 228 | | [UnityTest] |
| | 229 | | public IEnumerator VolumeIsUnmutedWhenUserEntersScene() |
| | 230 | | { |
| | 231 | | // We disable SceneController monobehaviour to avoid its current scene id update |
| 1 | 232 | | sceneController.enabled = false; |
| | 233 | |
|
| | 234 | | // Set current scene as a different one |
| 1 | 235 | | CommonScriptableObjects.sceneID.Set("unexistent-scene"); |
| | 236 | |
|
| 1 | 237 | | var entity = TestHelpers.CreateSceneEntity(scene); |
| 1 | 238 | | yield return null; |
| | 239 | |
|
| 1 | 240 | | yield return TestHelpers.CreateAudioSourceWithClipForEntity(entity); |
| | 241 | |
|
| 1 | 242 | | DCLAudioSource dclAudioSource = entity.gameObject.GetComponentInChildren<DCLAudioSource>(); |
| 1 | 243 | | yield return dclAudioSource.routine; |
| | 244 | |
|
| 1 | 245 | | AudioSource unityAudioSource = dclAudioSource.GetComponentInChildren<AudioSource>(); |
| | 246 | |
|
| | 247 | | // Set current scene with this scene's id |
| 1 | 248 | | CommonScriptableObjects.sceneID.Set(scene.sceneData.id); |
| | 249 | |
|
| | 250 | | // Check the volume |
| 1 | 251 | | Assert.AreEqual(unityAudioSource.volume, dclAudioSource.volume); |
| 1 | 252 | | } |
| | 253 | |
|
| | 254 | | [UnityTest] |
| | 255 | | public IEnumerator AudioStreamComponentCreation() |
| | 256 | | { |
| 1 | 257 | | var entity = TestHelpers.CreateSceneEntity(scene); |
| 1 | 258 | | DCLAudioStream.Model model = new DCLAudioStream.Model() |
| | 259 | | { |
| | 260 | | url = "https://audio.dcl.guru/radio/8110/radio.mp3", |
| | 261 | | playing = false, |
| | 262 | | volume = 1f |
| | 263 | | }; |
| 1 | 264 | | DCLAudioStream component = TestHelpers.EntityComponentCreate<DCLAudioStream, DCLAudioStream.Model>(scene, en |
| | 265 | |
|
| 1 | 266 | | yield return component.routine; |
| 1 | 267 | | Assert.IsFalse(component.GetModel().playing); |
| | 268 | |
|
| 1 | 269 | | model.playing = true; |
| 1 | 270 | | component.UpdateFromModel(model); |
| 1 | 271 | | yield return component.routine; |
| 1 | 272 | | Assert.IsTrue(component.GetModel().playing); |
| | 273 | |
|
| 1 | 274 | | model.playing = false; |
| 1 | 275 | | component.UpdateFromModel(model); |
| 1 | 276 | | yield return component.routine; |
| 1 | 277 | | Assert.IsFalse(component.GetModel().playing); |
| 1 | 278 | | } |
| | 279 | |
|
| | 280 | | [Test] |
| | 281 | | public void AudioClip_OnReadyBeforeLoading() |
| | 282 | | { |
| 1 | 283 | | DCLAudioClip dclAudioClip = CreateAudioClip(TestAssetsUtils.GetPath() + "/Audio/short_effect.ogg", true, tru |
| 1 | 284 | | bool isOnReady = false; |
| 3 | 285 | | dclAudioClip.CallWhenReady((x) => { isOnReady = true; }); |
| | 286 | |
|
| 1 | 287 | | Assert.IsTrue(isOnReady); //DCLAudioClip is ready on creation |
| 1 | 288 | | } |
| | 289 | |
|
| | 290 | | [UnityTest] |
| | 291 | | public IEnumerator AudioClip_OnReadyWaitLoading() |
| | 292 | | { |
| 1 | 293 | | DCLAudioClip dclAudioClip = CreateAudioClip(TestAssetsUtils.GetPath() + "/Audio/short_effect.ogg", true, tru |
| 1 | 294 | | bool isOnReady = false; |
| 3 | 295 | | dclAudioClip.CallWhenReady((x) => { isOnReady = true; }); |
| 1 | 296 | | yield return dclAudioClip.routine; |
| | 297 | |
|
| 1 | 298 | | Assert.IsTrue(isOnReady); |
| 1 | 299 | | } |
| | 300 | |
|
| | 301 | | [UnityTest] |
| | 302 | | public IEnumerator AudioClip_OnReadyAfterLoadingInstantlyCalled() |
| | 303 | | { |
| 1 | 304 | | DCLAudioClip dclAudioClip = CreateAudioClip(TestAssetsUtils.GetPath() + "/Audio/short_effect.ogg", true, tru |
| 1 | 305 | | yield return dclAudioClip.routine; |
| 1 | 306 | | bool isOnReady = false; |
| 3 | 307 | | dclAudioClip.CallWhenReady((x) => { isOnReady = true; }); |
| | 308 | |
|
| 1 | 309 | | Assert.IsTrue(isOnReady); |
| 1 | 310 | | } |
| | 311 | | } |
| | 312 | | } |