IAudioSessionControl2Test.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // -----------------------------------------
  2. // SoundScribe (TM) and related software.
  3. //
  4. // Copyright (C) 2007-2011 Vannatech
  5. // http://www.vannatech.com
  6. // All rights reserved.
  7. //
  8. // This source code is subject to the MIT License.
  9. // http://www.opensource.org/licenses/mit-license.php
  10. //
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. // THE SOFTWARE.
  18. // -----------------------------------------
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Runtime.InteropServices;
  22. using CoreAudioTests.Common;
  23. using Microsoft.VisualStudio.TestTools.UnitTesting;
  24. using Vannatech.CoreAudio.Constants;
  25. using Vannatech.CoreAudio.Interfaces;
  26. namespace CoreAudioTests.Wasapi
  27. {
  28. /// <summary>
  29. /// Tests all methods of the IAudioSessionControl2 interface.
  30. /// </summary>
  31. /// <remarks>
  32. /// This test class uses the context of IAudioSessionControl because
  33. /// that interface must be used to retrieve the IAudioSessionControl2 instance.
  34. /// </remarks>
  35. [TestClass]
  36. public class IAudioSessionControl2Test : TestClass<IAudioSessionControl>
  37. {
  38. /// <summary>
  39. /// Tests that the process ID can be received, for each applicable audio client.
  40. /// </summary>
  41. [TestMethod]
  42. public void IAudioSessionControl2_GetProcessId()
  43. {
  44. var tested = false;
  45. ExecuteServiceTest(service =>
  46. {
  47. var sc2 = ToAudioSessionControl2(service);
  48. if (sc2 == null) return;
  49. Manager.EnsureDisposal(sc2);
  50. var processId = UInt32.MaxValue;
  51. var result = sc2.GetProcessId(out processId);
  52. AssertCoreAudio.IsHResultOk(result);
  53. Assert.AreNotEqual(UInt32.MaxValue, processId, "The process ID was not received.");
  54. tested = true;
  55. });
  56. if (!tested) Assert.Inconclusive("No audio clients were found which supported the IAudioSessionControl2 interface.");
  57. }
  58. /// <summary>
  59. /// Tests that the session ID can be received, for each applicable audio client.
  60. /// </summary>
  61. [TestMethod]
  62. public void IAudioSessionControl2_GetSessionIdentifier()
  63. {
  64. var tested = false;
  65. ExecuteServiceTest(service =>
  66. {
  67. var sc2 = ToAudioSessionControl2(service);
  68. if (sc2 == null) return;
  69. Manager.EnsureDisposal(sc2);
  70. var sessionId = "abc123";
  71. var result = sc2.GetSessionIdentifier(out sessionId);
  72. AssertCoreAudio.IsHResultOk(result);
  73. Assert.AreNotEqual("abc123", sessionId, "The session ID was not received.");
  74. tested = true;
  75. });
  76. if (!tested) Assert.Inconclusive("No audio clients were found which supported the IAudioSessionControl2 interface.");
  77. }
  78. /// <summary>
  79. /// Tests that the session instance ID can be received, for each applicable audio client.
  80. /// </summary>
  81. [TestMethod]
  82. public void IAudioSessionControl2_GetSessionInstanceIdentifier()
  83. {
  84. var tested = false;
  85. ExecuteServiceTest(service =>
  86. {
  87. var sc2 = ToAudioSessionControl2(service);
  88. if (sc2 == null) return;
  89. Manager.EnsureDisposal(sc2);
  90. var instanceId = "abc123";
  91. var result = sc2.GetSessionInstanceIdentifier(out instanceId);
  92. AssertCoreAudio.IsHResultOk(result);
  93. Assert.AreNotEqual("abc123", instanceId, "The session instance ID was not received.");
  94. tested = true;
  95. });
  96. if (!tested) Assert.Inconclusive("No audio clients were found which supported the IAudioSessionControl2 interface.");
  97. }
  98. /// <summary>
  99. /// Tests that the system sound boolean flag can be received, for each applicable audio client.
  100. /// </summary>
  101. [TestMethod]
  102. public void IAudioSessionControl2_IsSystemSoundsSession()
  103. {
  104. var tested = false;
  105. ExecuteServiceTest(service =>
  106. {
  107. var sc2 = ToAudioSessionControl2(service);
  108. if (sc2 == null) return;
  109. Manager.EnsureDisposal(sc2);
  110. var result = sc2.IsSystemSoundsSession();
  111. Assert.IsTrue(result == 0 || result == 1, "The boolean flag is not within the valid range.");
  112. tested = true;
  113. });
  114. if (!tested) Assert.Inconclusive("No audio clients were found which supported the IAudioSessionControl2 interface.");
  115. }
  116. /// <summary>
  117. /// Tests that the ducking preference can be set, for each applicable audio client.
  118. /// </summary>
  119. [TestMethod]
  120. public void IAudioSessionControl2_SetDuckingPreference()
  121. {
  122. var tested = false;
  123. ExecuteServiceTest(service =>
  124. {
  125. var sc2 = ToAudioSessionControl2(service);
  126. if (sc2 == null) return;
  127. Manager.EnsureDisposal(sc2);
  128. var result = sc2.SetDuckingPreference(true);
  129. // Check for wrong endpoint type.
  130. // This method call is only valid for render devices.
  131. if ((uint)result == 0x88890003) return;
  132. AssertCoreAudio.IsHResultOk(result);
  133. result = sc2.SetDuckingPreference(false);
  134. AssertCoreAudio.IsHResultOk(result);
  135. tested = true;
  136. });
  137. if (!tested) Assert.Inconclusive("No audio clients were found which supported the IAudioSessionControl2 interface.");
  138. }
  139. /// <summary>
  140. /// Converts IAudioSessionControl to IAudioSessionControl2.
  141. /// </summary>
  142. /// <param name="sessionControl">The original interface.</param>
  143. /// <returns>A valid IAudioSessionControl2 instance, or null if one cannot be created.</returns>
  144. private IAudioSessionControl2 ToAudioSessionControl2(IAudioSessionControl sessionControl)
  145. {
  146. var sc2 = (IAudioSessionControl2)sessionControl;
  147. var result = sc2.IsSystemSoundsSession();
  148. if ((uint)result == TestUtilities.HRESULTS.E_NOINTERFACE)
  149. return null;
  150. return sc2;
  151. }
  152. }
  153. }