A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using System.Threading;
6using NUnit.Framework;
7using osu.Framework.Audio.Sample;
8using osu.Framework.Development;
9using osu.Framework.Threading;
10
11namespace osu.Framework.Tests.Audio
12{
13 [TestFixture]
14 public class SampleChannelVirtualTest
15 {
16 private Sample sample;
17
18 [SetUp]
19 public void Setup()
20 {
21 sample = new SampleVirtual();
22 updateSample();
23 }
24
25 [Test]
26 public void TestStart()
27 {
28 var channel = sample.Play();
29 Assert.IsTrue(channel.Playing);
30 Assert.IsFalse(channel.HasCompleted);
31
32 updateSample();
33
34 Assert.IsFalse(channel.Playing);
35 Assert.IsTrue(channel.HasCompleted);
36 }
37
38 [Test]
39 public void TestLooping()
40 {
41 var channel = sample.Play();
42 channel.Looping = true;
43 Assert.IsTrue(channel.Playing);
44 Assert.IsFalse(channel.HasCompleted);
45
46 updateSample();
47
48 Assert.IsTrue(channel.Playing);
49 Assert.False(channel.HasCompleted);
50
51 channel.Stop();
52
53 Assert.False(channel.Playing);
54 Assert.IsTrue(channel.HasCompleted);
55 }
56
57 private void updateSample() => RunOnAudioThread(() => sample.Update());
58
59 /// <summary>
60 /// Certain actions are invoked on the audio thread.
61 /// Here we simulate this process on a correctly named thread to avoid endless blocking.
62 /// </summary>
63 /// <param name="action">The action to perform.</param>
64 public static void RunOnAudioThread(Action action)
65 {
66 var resetEvent = new ManualResetEvent(false);
67
68 new Thread(() =>
69 {
70 ThreadSafety.IsAudioThread = true;
71
72 action();
73
74 resetEvent.Set();
75 })
76 {
77 Name = GameThread.PrefixedThreadNameFor("Audio")
78 }.Start();
79
80 if (!resetEvent.WaitOne(TimeSpan.FromSeconds(10)))
81 throw new TimeoutException();
82 }
83 }
84}