TestManager.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /// Delegate used to handle a test.
  33. /// </summary>
  34. /// <typeparam name="T">The interface type.</typeparam>
  35. /// <param name="testInterface">The interface instance.</param>
  36. public delegate void TestReadyDelegate<T>(T testInterface);
  37. /// <summary>
  38. /// Base class for all test managers.
  39. /// </summary>
  40. /// <typeparam name="T">The interface type.</typeparam>
  41. public abstract class TestManager<T>
  42. {
  43. // Internal collection of disposable items.
  44. private List<object> _disposables;
  45. /// <summary>
  46. /// The method called when a test is ready to be run.
  47. /// </summary>
  48. public TestReadyDelegate<T> OnTestReady
  49. {
  50. get;
  51. set;
  52. }
  53. /// <summary>
  54. /// Creats a new instance of the class.
  55. /// </summary>
  56. public TestManager()
  57. {
  58. _disposables = new List<object>();
  59. }
  60. /// <summary>
  61. /// Ensures that either IDisposable objects or COM objects are properly cleaned up.
  62. /// </summary>
  63. /// <param name="disposable">The object to dispose of.</param>
  64. public void EnsureDisposal(object obj)
  65. {
  66. var disposable = obj as IDisposable;
  67. if(obj != null || (disposable.GetType().Name.Contains("ComObject")))
  68. _disposables.Add(obj);
  69. }
  70. /// <summary>
  71. /// Begins running the tests.
  72. /// </summary>
  73. public void Run()
  74. {
  75. if (OnTestReady == null)
  76. throw new InvalidOperationException("The OnTestReady property must be set");
  77. try
  78. {
  79. OnRun();
  80. }
  81. finally
  82. {
  83. foreach (var d in _disposables)
  84. {
  85. var d2 = d as IDisposable;
  86. if (d2 != null) d2.Dispose();
  87. else Marshal.FinalReleaseComObject(d);
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// Method that implements the actual test iteration.
  93. /// </summary>
  94. protected abstract void OnRun();
  95. }
  96. }