ServiceTestManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /// Test manager used to run tests within the context of a AudioClientService object.
  33. /// </summary>
  34. /// <typeparam name="T">The interface type.</typeparam>
  35. public class ServiceTestManager<T> : TestManager<T>
  36. {
  37. /// <summary>
  38. /// Creats a new test manager instance.
  39. /// </summary>
  40. /// <typeparam name="T">The interface type.</typeparam>
  41. /// <param name="comIId">The COM IID of the interface.</param>
  42. /// <returns>A new service test manager instance.</returns>
  43. public static ServiceTestManager<T> Create(string comIId)
  44. {
  45. return new ServiceTestManager<T>(TestUtilities.CreateAudioClientServiceCollection<T>(comIId));
  46. }
  47. /// <summary>
  48. /// Creats a new test manager instance.
  49. /// </summary>
  50. /// <typeparam name="T">The interface type.</typeparam>
  51. /// <param name="comIId">The COM IID of the interface.</param>
  52. /// <param name="exclusiveMode">A value indicating whether or not to use exclusive mode.</param>
  53. /// <returns>A new service test manager instance.</returns>
  54. public static ServiceTestManager<T> Create(string comIId, bool exclusiveMode)
  55. {
  56. return new ServiceTestManager<T>(TestUtilities.CreateAudioClientServiceCollection<T>(comIId, exclusiveMode));
  57. }
  58. /// <summary>
  59. /// Creats a new test manager instance.
  60. /// </summary>
  61. /// <typeparam name="T">The interface type.</typeparam>
  62. /// <param name="comIId">The COM IID of the interface.</param>
  63. /// <param name="exclusiveMode">A value indicating whether or not to use exclusive mode.</param>
  64. /// <param name="streamFlags">The stream flags to use during initialization.</param>
  65. /// <returns>A new service test manager instance.</returns>
  66. public static ServiceTestManager<T> Create(string comIId, bool exclusiveMode, UInt32 streamFlags)
  67. {
  68. return new ServiceTestManager<T>(TestUtilities.CreateAudioClientServiceCollection<T>(comIId, exclusiveMode, streamFlags));
  69. }
  70. /// <summary>
  71. /// A list of audio client service objects to test against.
  72. /// </summary>
  73. protected IEnumerable<AudioClientService<T>> Items
  74. {
  75. get;
  76. set;
  77. }
  78. /// <summary>
  79. /// Indicates whether or not to start the audio client before running a test.
  80. /// </summary>
  81. public bool AutoStartClient
  82. {
  83. get;
  84. set;
  85. }
  86. /// <summary>
  87. /// Creates a new instance of the class.
  88. /// </summary>
  89. /// <param name="items">A list of device activations to use.</param>
  90. public ServiceTestManager(IEnumerable<AudioClientService<T>> items)
  91. {
  92. Items = items;
  93. }
  94. /// <summary>
  95. /// Runs the tests.
  96. /// </summary>
  97. protected override void OnRun()
  98. {
  99. var isTested = false;
  100. foreach (var i in Items)
  101. {
  102. try
  103. {
  104. var isRunning = false;
  105. if (AutoStartClient)
  106. {
  107. var startResult = i.AudioClient.Start();
  108. if (startResult != 0) continue;
  109. // Slight delay is required.
  110. System.Threading.Thread.Sleep(100);
  111. isRunning = true;
  112. }
  113. OnTestReady(i.ServiceInterface);
  114. if (isRunning) i.AudioClient.Stop();
  115. isTested = true;
  116. }
  117. finally
  118. {
  119. EnsureDisposal(i);
  120. }
  121. }
  122. if (!isTested) Assert.Inconclusive("No valid services were found to test against.");
  123. }
  124. }
  125. }