IAudioClientTest.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.Enumerations;
  26. using Vannatech.CoreAudio.Interfaces;
  27. namespace CoreAudioTests.Wasapi
  28. {
  29. /// <summary>
  30. /// Tests all methods of the IAudioClient interface.
  31. /// </summary>
  32. [TestClass]
  33. public class IAudioClientTest : TestClass<IAudioClient>
  34. {
  35. /// <summary>
  36. /// Tests that the buffer size may be received, for each available audio client.
  37. /// </summary>
  38. [TestMethod]
  39. public void IAudioClient_GetBufferSize()
  40. {
  41. ExecuteCustomTest(new AudioClientTestManager(), ac =>
  42. {
  43. var size = UInt32.MaxValue;
  44. var result = ac.GetBufferSize(out size);
  45. AssertCoreAudio.IsHResultOk(result);
  46. Assert.AreNotEqual(UInt32.MaxValue, size, "The size was not received.");
  47. });
  48. }
  49. /// <summary>
  50. /// Tests that the buffer size may be received, for each available audio client.
  51. /// </summary>
  52. [TestMethod]
  53. public void IAudioClient_GetCurrentPadding()
  54. {
  55. ExecuteCustomTest(new AudioClientTestManager(), ac =>
  56. {
  57. var count = UInt32.MaxValue;
  58. var result = ac.GetCurrentPadding(out count);
  59. AssertCoreAudio.IsHResultOk(result);
  60. Assert.AreNotEqual(UInt32.MaxValue, count, "The padding frame count was not received.");
  61. });
  62. }
  63. /// <summary>
  64. /// Tests that the device period values may be received, for each available audio client.
  65. /// </summary>
  66. [TestMethod]
  67. public void IAudioClient_GetDevicePeriod()
  68. {
  69. ExecuteCustomTest(new AudioClientTestManager(), ac =>
  70. {
  71. var procInterval = UInt64.MaxValue;
  72. var minInterval = UInt64.MaxValue;
  73. var result = ac.GetDevicePeriod(out procInterval, out minInterval);
  74. AssertCoreAudio.IsHResultOk(result);
  75. Assert.AreNotEqual(UInt64.MaxValue, procInterval, "The process interval was not received.");
  76. Assert.AreNotEqual(UInt64.MaxValue, minInterval, "The minimum interval was not received.");
  77. });
  78. }
  79. /// <summary>
  80. /// Tests that the mix format may be received, for each available audio client.
  81. /// </summary>
  82. [TestMethod]
  83. public void IAudioClient_GetMixFormat()
  84. {
  85. ExecuteCustomTest(new AudioClientTestManager(), ac =>
  86. {
  87. var formatPtr = IntPtr.Zero;
  88. var result = ac.GetMixFormat(out formatPtr);
  89. AssertCoreAudio.IsHResultOk(result);
  90. Assert.AreNotEqual(IntPtr.Zero, formatPtr, "The mix format pointer was not received.");
  91. });
  92. }
  93. /// <summary>
  94. ///
  95. /// </summary>
  96. [TestMethod]
  97. public void IAudioClient_GetService()
  98. {
  99. // Testing of GetService method is implied, as this method is required for all other tests.
  100. // TODO: Low priority - add tests here to specifically get each available service.
  101. }
  102. /// <summary>
  103. /// Tests that the stream latency may be received, for each available audio client.
  104. /// </summary>
  105. [TestMethod]
  106. public void IAudioClient_GetStreamLatency()
  107. {
  108. ExecuteCustomTest(new AudioClientTestManager(), ac =>
  109. {
  110. var latency = UInt64.MaxValue;
  111. var result = ac.GetStreamLatency(out latency);
  112. AssertCoreAudio.IsHResultOk(result);
  113. Assert.AreNotEqual(UInt64.MaxValue, latency, "The stream latency was not received.");
  114. });
  115. }
  116. /// <summary>
  117. ///
  118. /// </summary>
  119. [TestMethod]
  120. public void IAudioClient_Initialize()
  121. {
  122. // Testing of Initialize method is implied, as this method is required for all other tests.
  123. // TODO: Low priority - add tests produce WASAPI errors, anad verify expected error codes.
  124. }
  125. /// <summary>
  126. ///
  127. /// </summary>
  128. [TestMethod]
  129. public void IAudioClient_IsFormatSupported()
  130. {
  131. // Testing of IsFormatSupported method is implied, as this method is required for all other tests.
  132. // TODO: Low priority - add a more robust test with a wide range of formats.
  133. }
  134. /// <summary>
  135. /// Tests that the Start method executes properly, for each available audio client.
  136. /// </summary>
  137. [TestMethod]
  138. public void IAudioClient_Reset()
  139. {
  140. ExecuteCustomTest(new AudioClientTestManager(), ac =>
  141. {
  142. ac.Start();
  143. var result = ac.Reset();
  144. Assert.AreNotEqual(0, result, "The audio client successfully reset, but was not stopped.");
  145. ac.Stop();
  146. result = ac.Reset();
  147. AssertCoreAudio.IsHResultOk(result);
  148. });
  149. }
  150. /// <summary>
  151. ///
  152. /// </summary>
  153. [TestMethod]
  154. public void IAudioClient_SetEventHandle()
  155. {
  156. Assert.Fail("TODO: Implement test for SetEventHandle method");
  157. }
  158. /// <summary>
  159. /// Tests that the Start method executes properly, for each available audio client.
  160. /// </summary>
  161. [TestMethod]
  162. public void IAudioClient_Start()
  163. {
  164. ExecuteCustomTest(new AudioClientTestManager(), ac =>
  165. {
  166. var result = ac.Start();
  167. AssertCoreAudio.IsHResultOk(result);
  168. result = ac.Start();
  169. Assert.AreNotEqual(0, result, "The audio client successfully started, but was already running.");
  170. });
  171. }
  172. /// <summary>
  173. /// Tests that the Stop method executes properly, for each available audio client.
  174. /// </summary>
  175. [TestMethod]
  176. public void IAudioClient_Stop()
  177. {
  178. ExecuteCustomTest(new AudioClientTestManager(), ac =>
  179. {
  180. ac.Start();
  181. var result = ac.Stop();
  182. AssertCoreAudio.IsHResultOk(result);
  183. // A second call to Stop should fail.
  184. result = ac.Stop();
  185. Assert.AreNotEqual(0, result, "The audio client successfully stopped, but was not started.");
  186. });
  187. }
  188. /// <summary>
  189. /// Test manager specific to audio client testing.
  190. /// </summary>
  191. private class AudioClientTestManager : DeviceActivationTestManager<IAudioClient>
  192. {
  193. /// <summary>
  194. /// Creates a new instance of the class.
  195. /// </summary>
  196. public AudioClientTestManager()
  197. : base(TestUtilities.CreateDeviceActivationCollection<IAudioClient>(ComIIDs.IAudioClientIID))
  198. {
  199. }
  200. protected override void OnRun()
  201. {
  202. // Begins with shared mode testing
  203. // Then runs again in exclusive mode
  204. var shareMode = AUDCLNT_SHAREMODE.AUDCLNT_SHAREMODE_SHARED;
  205. testStart:
  206. var tested = false;
  207. try
  208. {
  209. foreach (var activation in Items)
  210. {
  211. var formatPtr = TestUtilities.GetFormatPointer(activation.ActiveInterface, shareMode);
  212. if (formatPtr == IntPtr.Zero) continue;
  213. var context = Guid.NewGuid();
  214. var init = activation.ActiveInterface.Initialize(shareMode, 0, 5000000, 0, formatPtr, context);
  215. if (init != 0) continue;
  216. OnTestReady(activation.ActiveInterface);
  217. tested = true;
  218. }
  219. }
  220. finally
  221. {
  222. foreach (var activation in Items)
  223. EnsureDisposal(activation);
  224. }
  225. if (!tested) Assert.Inconclusive("No valid audio clients were tested.");
  226. if (shareMode == AUDCLNT_SHAREMODE.AUDCLNT_SHAREMODE_SHARED)
  227. {
  228. // Get items and retest in exclusive mode.
  229. Items = TestUtilities.CreateDeviceActivationCollection<IAudioClient>(ComIIDs.IAudioClientIID);
  230. shareMode = AUDCLNT_SHAREMODE.AUDCLNT_SHAREMODE_EXCLUSIVE;
  231. goto testStart;
  232. }
  233. }
  234. }
  235. }
  236. }