TestClass.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.Linq;
  22. using System.Runtime.InteropServices;
  23. using System.Text;
  24. using Microsoft.VisualStudio.TestTools.UnitTesting;
  25. using Vannatech.CoreAudio.Constants;
  26. using Vannatech.CoreAudio.Enumerations;
  27. using Vannatech.CoreAudio.Externals;
  28. using Vannatech.CoreAudio.Interfaces;
  29. namespace CoreAudioTests.Common
  30. {
  31. /// <summary>
  32. /// Base class for tests that utilize MMDevice Activation or AudioClient Services.
  33. /// </summary>
  34. /// <typeparam name="T">The interface type.</typeparam>
  35. public abstract class TestClass<T>
  36. {
  37. /// <summary>
  38. /// The COM IID.
  39. /// </summary>
  40. protected virtual string ComIID
  41. {
  42. get
  43. {
  44. var interfaceName = typeof(T).Name;
  45. var fieldInfo = typeof(ComIIDs).GetField(interfaceName + "IID");
  46. if (fieldInfo == null)
  47. throw new ArgumentException("Could not find the COM IID for the specified ");
  48. return fieldInfo.GetValue(null) as string;
  49. }
  50. }
  51. /// <summary>
  52. /// Gets the test manager used to run tests.
  53. /// </summary>
  54. protected TestManager<T> Manager
  55. {
  56. get;
  57. private set;
  58. }
  59. /// <summary>
  60. /// Runs tests via a custom test manager.
  61. /// </summary>
  62. /// <param name="testManager">The test manager.</param>
  63. /// <param name="onTestReady">The test method to use.</param>
  64. protected void ExecuteCustomTest(TestManager<T> testManager, TestReadyDelegate<T> onTestReady)
  65. {
  66. Manager = testManager;
  67. Manager.OnTestReady = onTestReady;
  68. Manager.Run();
  69. }
  70. /// <summary>
  71. /// Runs each test, creating the specified interface from the IMMDevice.Activate method.
  72. /// </summary>
  73. /// <param name="onTestReady">The test method to use.</param>
  74. protected virtual void ExecuteDeviceActivationTest(TestReadyDelegate<T> onTestReady)
  75. {
  76. Manager = DeviceActivationTestManager<T>.Create(ComIID);
  77. Manager.OnTestReady = onTestReady;
  78. Manager.Run();
  79. }
  80. /// <summary>
  81. /// Runs each test, creating the specified interface from the IPart.Activate method.
  82. /// </summary>
  83. /// <param name="onTestReady">The test method to use.</param>
  84. protected virtual void ExecutePartActivationTest(TestReadyDelegate<T> onTestReady)
  85. {
  86. Manager = PartActivationTestManager<T>.Create(ComIID);
  87. Manager.OnTestReady = onTestReady;
  88. Manager.Run();
  89. }
  90. /// <summary>
  91. /// Runs each test, creating the specified interface from the IAudioClient.GetService method.
  92. /// Tests are run in both shared and exclusive mode.
  93. /// </summary>
  94. /// <param name="onTestReady">The test method to use.</param>
  95. protected virtual void ExecuteServiceTest(TestReadyDelegate<T> onTestReady)
  96. {
  97. Manager = ServiceTestManager<T>.Create(ComIID, false);
  98. Manager.OnTestReady = onTestReady;
  99. Manager.Run();
  100. Manager = ServiceTestManager<T>.Create(ComIID, true);
  101. Manager.OnTestReady = onTestReady;
  102. Manager.Run();
  103. }
  104. /// <summary>
  105. /// Runs each test, creating the specified interface from the IAudioClient.GetService method.
  106. /// Tests are run only in the specified share mode.
  107. /// </summary>
  108. /// <param name="shareMode">The audio client share mode.</param>
  109. /// <param name="onTestReady">The test method to use.</param>
  110. protected virtual void ExecuteServiceTest(AUDCLNT_SHAREMODE shareMode, TestReadyDelegate<T> onTestReady)
  111. {
  112. var exclusiveMode = (shareMode == AUDCLNT_SHAREMODE.AUDCLNT_SHAREMODE_EXCLUSIVE);
  113. Manager = ServiceTestManager<T>.Create(ComIID, exclusiveMode);
  114. Manager.OnTestReady = onTestReady;
  115. Manager.Run();
  116. }
  117. /// <summary>
  118. /// Runs each test, creating the specified interface from the IAudioClient.GetService method.
  119. /// Tests are run in both shared and exclusive mode, with the audio client in the running (started) state.
  120. /// </summary>
  121. /// <param name="onTestReady">The test method to use.</param>
  122. protected virtual void ExecuteRunningServiceTest(TestReadyDelegate<T> onTestReady)
  123. {
  124. var serviceTestManager = ServiceTestManager<T>.Create(ComIID, false);
  125. serviceTestManager.AutoStartClient = true;
  126. Manager = serviceTestManager;
  127. Manager.OnTestReady = onTestReady;
  128. Manager.Run();
  129. serviceTestManager = ServiceTestManager<T>.Create(ComIID, true);
  130. serviceTestManager.AutoStartClient = true;
  131. Manager = serviceTestManager;
  132. Manager.OnTestReady = onTestReady;
  133. Manager.Run();
  134. }
  135. /// <summary>
  136. /// Runs each test, creating the specified interface from the IAudioClient.GetService method.
  137. /// Tests are run only in the specified share mode, with the audio client in the running (started) state.
  138. /// </summary>
  139. /// <param name="shareMode">The audio client share mode.</param>
  140. /// <param name="onTestReady">The test method to use.</param>
  141. protected virtual void ExecuteRunningServiceTest(AUDCLNT_SHAREMODE shareMode, TestReadyDelegate<T> onTestReady)
  142. {
  143. var exclusiveMode = (shareMode == AUDCLNT_SHAREMODE.AUDCLNT_SHAREMODE_EXCLUSIVE);
  144. var serviceTestManager = ServiceTestManager<T>.Create(ComIID, exclusiveMode);
  145. serviceTestManager.AutoStartClient = true;
  146. Manager = serviceTestManager;
  147. Manager.OnTestReady = onTestReady;
  148. Manager.Run();
  149. }
  150. }
  151. }