IMMDeviceCollectionTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 CoreAudioTests.Common;
  21. using Microsoft.VisualStudio.TestTools.UnitTesting;
  22. using Vannatech.CoreAudio.Constants;
  23. using Vannatech.CoreAudio.Enumerations;
  24. using Vannatech.CoreAudio.Interfaces;
  25. namespace CoreAudioTests.MMDeviceApi
  26. {
  27. /// <summary>
  28. /// Tests all methods of the IMMDeviceCollection interface.
  29. /// </summary>
  30. [TestClass]
  31. public class IMMDeviceCollectionTest
  32. {
  33. /// <summary>
  34. /// Tests that the individual render and capture device collections have a combined count equal to the total device count.
  35. /// </summary>
  36. [TestMethod]
  37. public void IMMDeviceCollection_GetCount()
  38. {
  39. int result = 0;
  40. var enumerator = TestUtilities.CreateIMMDeviceEnumerator();
  41. IMMDeviceCollection allCaptureDevices;
  42. IMMDeviceCollection allRenderDevices;
  43. IMMDeviceCollection allDevices;
  44. enumerator.EnumAudioEndpoints(EDataFlow.eCapture, DEVICE_STATE_XXX.DEVICE_STATEMASK_ALL, out allCaptureDevices);
  45. enumerator.EnumAudioEndpoints(EDataFlow.eRender, DEVICE_STATE_XXX.DEVICE_STATEMASK_ALL, out allRenderDevices);
  46. enumerator.EnumAudioEndpoints(EDataFlow.eAll, DEVICE_STATE_XXX.DEVICE_STATEMASK_ALL, out allDevices);
  47. Assert.IsNotNull(allCaptureDevices, "The IMMDeviceCollection object is null.");
  48. Assert.IsNotNull(allRenderDevices, "The IMMDeviceCollection object is null.");
  49. Assert.IsNotNull(allDevices, "The IMMDeviceCollection object is null.");
  50. UInt32 captureCount = UInt32.MaxValue, renderCount = UInt32.MaxValue, allCount = UInt32.MaxValue;
  51. result = allCaptureDevices.GetCount(out captureCount);
  52. AssertCoreAudio.IsHResultOk(result);
  53. Assert.AreNotEqual(UInt32.MaxValue, captureCount, "Device count was not received.");
  54. result = allRenderDevices.GetCount(out renderCount);
  55. AssertCoreAudio.IsHResultOk(result);
  56. Assert.AreNotEqual(UInt32.MaxValue, renderCount, "Device count was not received.");
  57. result = allDevices.GetCount(out allCount);
  58. AssertCoreAudio.IsHResultOk(result);
  59. Assert.AreNotEqual(UInt32.MaxValue, allDevices, "Device count was not received.");
  60. Assert.AreEqual(allCount, captureCount + renderCount, "The combined number of capture and render devices is not equal to the total device count.");
  61. }
  62. /// <summary>
  63. /// Tests the all devices from index zero to [count - 1] can be received with S_OK HRESULT and each device is not null.
  64. /// </summary>
  65. [TestMethod]
  66. public void IMMDeviceCollection_Item()
  67. {
  68. int result = 0;
  69. var enumerator = TestUtilities.CreateIMMDeviceEnumerator();
  70. IMMDeviceCollection allDevices;
  71. result = enumerator.EnumAudioEndpoints(EDataFlow.eAll, DEVICE_STATE_XXX.DEVICE_STATEMASK_ALL, out allDevices);
  72. AssertCoreAudio.IsHResultOk(result);
  73. Assert.IsNotNull(allDevices, "The IMMDeviceCollection object is null");
  74. UInt32 count;
  75. allDevices.GetCount(out count);
  76. for (uint i = 0; i < count; i++)
  77. {
  78. IMMDevice device;
  79. result = allDevices.Item(i, out device);
  80. AssertCoreAudio.IsHResultOk(result);
  81. }
  82. }
  83. }
  84. }